我正在尝试设置一个基本的模块化程序,但是在导入模块时遇到了问题。当我尝试导入自定义模块时,我得到以下错误:
(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或我的某个文件中设置了错误?
***编辑:我正在使用node testCase.js
运行testCase.js
。
testStep
里有一个多余的括号或逗号。 - Matthewimport
不能在函数内部。 - Jaromanda Xes2015
。 - Matthew