nodejs,export报错SyntaxError: Unexpected token export

栏目: Node.js · 发布时间: 7年前

内容简介:最近做一个项目,写nodejs,又写vue,然后就有点混乱了。当时想引入一个config文件,然后很自然的啪啪啪的敲了下面几行代码

最近做一个项目,写nodejs,又写vue,然后就有点混乱了。

当时想引入一个config文件,然后很自然的啪啪啪的敲了下面几行代码

//错误代码
export default const config = {
    static_img_url: 'http://localhost:7001/images/'
}

nodejs,export报错SyntaxError: Unexpected token export

啪啦啪啦,查了一下,说原因是export default中的default是一种特殊的系统变量,export default的含义是把此命令后面的变量赋值给default这个特殊的系统变量,并把它导出到其他模块中使用。如此一来,export default const...或者export default var...等语句就是非常明显的错误了。

好,改了一下如下:

//还是错误代码
const config = {
    static_img_url: 'http://localhost:7001/images/'
}
 export default config

还是报错:

nodejs,export报错SyntaxError: Unexpected token export

为什么呢???发现掉坑了,搞混乱了。正确应该如下:

//正确代码
const config = {
    static_img_url: 'http://localhost:7001/images/'
}
module.exports = config

其实就是Node和浏览器端所支持的模块规范不同。

nodejs,export报错SyntaxError: Unexpected token export

  1. 关于exports和module.exports
    在一个node执行一个文件时,会给这个文件内生成一个 exports和module对象,而module有一个exports属性。
    exports = module.exports = {};
  2. 关于 export 和export default
    export与export default均可用于导出常量、函数、文件、模块等
    在一个文件或模块中,export、import可以有多个,export default仅有一个
    通过export方式导出,在导入时要加{ },export default则不需要
    export能直接导出变量表达式,export default不行。

参考引用: 引用


以上所述就是小编给大家介绍的《nodejs,export报错SyntaxError: Unexpected token export》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

JavaScript Patterns

JavaScript Patterns

Stoyan Stefanov / O'Reilly Media, Inc. / 2010-09-21 / USD 29.99

What's the best approach for developing an application with JavaScript? This book helps you answer that question with numerous JavaScript coding patterns and best practices. If you're an experienced d......一起来看看 《JavaScript Patterns》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具