内容简介:最近做一个项目,写nodejs,又写vue,然后就有点混乱了。当时想引入一个config文件,然后很自然的啪啪啪的敲了下面几行代码
最近做一个项目,写nodejs,又写vue,然后就有点混乱了。
当时想引入一个config文件,然后很自然的啪啪啪的敲了下面几行代码
//错误代码
export default const config = {
static_img_url: 'http://localhost:7001/images/'
}
啪啦啪啦,查了一下,说原因是export default中的default是一种特殊的系统变量,export default的含义是把此命令后面的变量赋值给default这个特殊的系统变量,并把它导出到其他模块中使用。如此一来,export default const...或者export default var...等语句就是非常明显的错误了。
好,改了一下如下:
//还是错误代码
const config = {
static_img_url: 'http://localhost:7001/images/'
}
export default config
还是报错:
为什么呢???发现掉坑了,搞混乱了。正确应该如下:
//正确代码
const config = {
static_img_url: 'http://localhost:7001/images/'
}
module.exports = config
其实就是Node和浏览器端所支持的模块规范不同。
- 关于exports和module.exports
在一个node执行一个文件时,会给这个文件内生成一个 exports和module对象,而module有一个exports属性。
exports = module.exports = {}; - 关于 export 和export default
export与export default均可用于导出常量、函数、文件、模块等
在一个文件或模块中,export、import可以有多个,export default仅有一个
通过export方式导出,在导入时要加{ },export default则不需要
export能直接导出变量表达式,export default不行。
参考引用: 引用
以上所述就是小编给大家介绍的《nodejs,export报错SyntaxError: Unexpected token export》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Machine Learning in Action
Peter Harrington / Manning Publications / 2012-4-19 / GBP 29.99
It's been said that data is the new "dirt"—the raw material from which and on which you build the structures of the modern world. And like dirt, data can seem like a limitless, undifferentiated mass. ......一起来看看 《Machine Learning in Action》 这本书的介绍吧!