Javascript Babel ES6 导入错误,SyntaxError: Unexpected token import

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45293311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:59:50  来源:igfitidea点击:

Babel ES6 import error, SyntaxError: Unexpected token import

javascriptnode.jsbabeljs

提问by Graeham Broda

I am trying to set up a basic modular program, however I seem to be running into issues with importing modules. I attempt to import my custom module, I get the following error:

我正在尝试设置一个基本的模块化程序,但是我似乎遇到了导入模块的问题。我尝试导入我的自定义模块,但出现以下错误:

(function (exports, require, module, __filename, __dirname) { import testStep from 'testStep';
                                                          ^^^^^^
SyntaxError: Unexpected token import

The code that is causing the issue:

导致问题的代码:

testcase.js

测试用例.js

import testStep from 'testStep';

testStep.hello();

testStep.js

测试步骤.js

var testStep = {
  hello: hello,
};

var hello = () => {
  console.log('hello world');
};

export default {testStep};

package.json

包.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

.babelrc

{
  "presets": [
    "env"
  ]
}

I have already tried several other fixes, such as setting testStepas a class, as well as using require('./testStep.js'), however neither of those seem to have worked.

我已经尝试了其他几个修复程序,例如设置testStep为类,以及使用require('./testStep.js'),但是这些似乎都不起作用。

Do I have something set up incorrectly with babel or in one of my files?

我是否在使用 babel 或我的文件之一中设置了错误?

***Edit: I am running testCase.jswith node testCase.js.

***编辑:我正在testCase.js使用node testCase.js

采纳答案by danbruegge

Please install babel-cliand call your file with: ./node_modules/.bin/babel-node testcase.js

请安装babel-cli并调用您的文件:./node_modules/.bin/babel-node testcase.js

It will fail. Now we have to fix your code.

它会失败。现在我们必须修复您的代码。

testStep.js should look like:

testStep.js 应如下所示:

var hello = () => {
  console.log('hello world');
};

var testStep = {
  hello: hello,
};

export default testStep;

Then, it will work. ;)

然后,它将起作用。;)

This first introduction on https://babeljs.io/is, that you should install babel-cliand babel-preset-env. ;)

这对首次引入https://babeljs.io/是,你应该安装babel-clibabel-preset-env。;)

You can also write your testStep.js like this:

您还可以像这样编写 testStep.js:

var testStep = {
  hello: hello,
};

function hello () {
  console.log('hello world');
};

export default testStep;

This keeps the hoisting in mind. Like Jacobsaid in his first point.

这要牢记起重。就像雅各布在他的第一点所说的那样。

回答by Jijo Paulose

From the babel 6 Release notes:

来自 babel 6 发行说明:

Plugin Presets

插件预设

$ npm install --save-dev babel-preset-env

save it .babelrc file

保存 .babelrc 文件

{
  presets: ["env"]
}

Note: https://babeljs.io/docs/en/babel-preset-env#docsNav

注意:https: //babeljs.io/docs/en/babel-preset-env#docsNav

回答by Jacob

  1. You need to define hellowith function, using var, you will not have function hoisting, so when declaring testStep, hellois undefined.

  2. If you want to use es module, you can use babel-registeror babel-node. In your code, node can not handle es module.

  1. 你需要定义hellowith function, using var,你不会有function hoisting,所以在声明时testStep, ,hello是未定义的。

  2. 如果要使用 es 模块,可以使用babel-registerbabel-node。在您的代码中,node 无法处理 es 模块。

Babel-register

Babel 寄存器

With bebel-register, all your module will be handled by babel when they are imported.

使用 bebel-register,你的所有模块在导入时都会被 babel 处理。

First,yarn add babel-register babel-cli --devor npm install babel-register babel-cli -devThen create an entry file:

首先,yarn add babel-register babel-cli --dev或者npm install babel-register babel-cli -dev然后创建一个入口文件:

// entry.js
require('babel-register')
require('testcase')

In your testcase, you can use es module now.

在您的测试用例中,您现在可以使用 es 模块。

Edit you package.json:

编辑你的 package.json:

"scripts": {
    "test": "node entry.js"
},

you can run yarn testor npm run testin terminal.

您可以运行yarn testnpm run test在终端中。

You don't need babel-polyfill, that is for browsers, not for node.

您不需要babel-polyfill,即用于浏览器,而不是用于节点。