nodejs,export报错SyntaxError: Unexpected token export

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

内容简介:最近做一个项目,写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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Scalable Internet Architectures

Scalable Internet Architectures

Theo Schlossnagle / Sams Publishing / 2006-7-31 / USD 49.99

As a developer, you are aware of the increasing concern amongst developers and site architects that websites be able to handle the vast number of visitors that flood the Internet on a daily basis. Sc......一起来看看 《Scalable Internet Architectures》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具