【发布时间】:2017-12-30 18:58:14
【问题描述】:
我正在尝试设置一个基本的模块化程序,但是我似乎遇到了导入模块的问题。我尝试导入我的自定义模块,我收到以下错误:
(function (exports, require, module, __filename, __dirname) { import testStep from 'testStep';
^^^^^^
SyntaxError: Unexpected token import
导致问题的代码:
testcase.js
import testStep from 'testStep';
testStep.hello();
testStep.js
var testStep = {
hello: hello,
};
var hello = () => {
console.log('hello world');
};
export default {testStep};
package.json
{
"name": "rebuild-poc",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.6.0"
},
"dependencies": {}
}
.babelrc
{
"presets": [
"env"
]
}
我已经尝试了其他几个修复,例如将testStep 设置为一个类,以及使用require('./testStep.js'),但是这些似乎都没有奏效。
我是否在 babel 或我的某个文件中设置了不正确的内容?
***编辑:我正在运行 testCase.js 和 node testCase.js。
【问题讨论】:
-
testStep中似乎有一个额外的括号或额外的逗号 -
import不能在函数内部 -
@Matthew 在 es6 尾随逗号是允许的,但我尝试删除它并没有解决问题。
-
@JaromandaX 你能再澄清一下吗?据我所知,导入不在函数内部。它只是在脚本中,但也许我误解了
-
它在 IIFE 中(根据错误消息)....
Import declarations are only allowed at the top level of module scope...我可能误解了您现在得到的错误输出,因为我已经更仔细地查看了问题中的代码
标签: javascript node.js babeljs