node.js 节点错误:SyntaxError:意外的令牌导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37634198/
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-09-02 20:17:20  来源:igfitidea点击:

Node error: SyntaxError: Unexpected token import

node.jsbabel

提问by Stefdelec

I don't understand what is wrong. I checked other forum talking about transpilation and babel. What do I have to do?

我不明白出了什么问题。我查看了其他讨论转译和 babel 的论坛。我需要做什么?

node -v
v5.5.0

my code:

我的代码:

import recast from 'recastai'

and the error

和错误

(function (exports, require, module, __filename, __dirname) { import recast from 'module1'
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:139:18)
    at node.js:999:3

采纳答案by lorefnon

ES6 imports are a recently introduced feature and the current stable version of Node does not support them yet. Node.js issue tracker has an open issuefor this - but until V8 and Node add support for this feature, you will need to use a transpiler (most popular one being babel) to be able to use imports.

ES6 导入是最近引入的功能,当前稳定版的 Node 尚不支持它们。Node.js 问题跟踪器对此有一个未解决的问题- 但在 V8 和 Node 添加对此功能的支持之前,您将需要使用转译器(最流行的一个是babel)才能使用导入。

For quickly trying out transpilation, babel provides a web based REPL. This onedemonstrates your code being transpiled.

为了快速尝试转译,babel 提供了一个基于 Web 的 REPL。这个演示了您的代码正在被转译。

The babel project homepage points to the relevant resources for getting started with Babel and integrating it with your development workflow.

巴贝尔项目主页指向为相关资源入门巴贝尔和你的开发工作流程相集成。

For the simplest setup, visit this setup pageand select CLI in the Babel built-ins section.

对于最简单的设置,请访问此设置页面并在 Babel 内置部分中选择 CLI。

This basically involves three simple steps:

这基本上包括三个简单的步骤:

  1. Install babel-cli : npm install --save-dev babel-cli babel-preset-es2015
  2. Create .babelrcconfiguration file: echo '{ "presets": ["es2015"] }' > .babelrc
  3. Use the installed module to transpile your source code: ./node_modules/.bin/babel src -d lib
  1. 安装 babel-cli : npm install --save-dev babel-cli babel-preset-es2015
  2. 创建.babelrc配置文件:echo '{ "presets": ["es2015"] }' > .babelrc
  3. 使用安装的模块来转译你的源代码: ./node_modules/.bin/babel src -d lib

The aforementioned setup page also illustrates how to add an npm script to simplify the last step. Alternatively you can integrate babel with your editor or build chain so that your files are automatically compiled on change.

上述设置页面还说明了如何添加 npm 脚本以简化最后一步。或者,您可以将 babel 与您的编辑器或构建链集成,以便您的文件在更改时自动编译。

回答by Mahendran

In case you don't want to deal with babel. This one worked for me.

如果你不想处理 babel。这个对我有用。

const calc = require('./my_calc');
let {add, multiply} = calc;

回答by Jerome Anthony

1) Install the latest presets

1) 安装最新的预设

yarn add --dev babel-preset-latest

2) Create .babelrcand add the following

2)创建.babelrc并添加以下内容

{
    "presets": ["latest"]
}

3) Run your script

3)运行你的脚本

npx babel-node yourscript.js

Or in your package.jsonfile add

或者在你的package.json文件中添加

"scripts": {
  "start": "babel-node index.js"
}

回答by KARTHIKEYAN.A

Getting Started

入门

First we'll install babel-cli.

首先,我们将安装 babel-cli。

$ npm install --save-dev babel-cli

Along with some presets.

以及一些预设。

$ npm install --save-dev babel-preset-es2015 babel-preset-stage-2

package.json:

包.json:

  "scripts": {
    "start": "babel-node index.js --presets es2015,stage-2"
  }

run:

跑:

$ npm start

Watching file changes with nodemon:

使用nodemon观察文件更改:

We can improve our npm start script with nodemon.

我们可以使用 nodemon 改进我们的 npm start 脚本。

$ npm install --save-dev nodemon

Then we can update our npm start script.

然后我们可以更新我们的 npm start 脚本。

package.json:

包.json:

 "scripts": {
   "start": "nodemon index.js --exec babel-node --presets es2015,stage-2"
  }

run:

跑:

$ npm start 

If you are using pm2, then follow these steps:

如果您使用的是pm2,请按照以下步骤操作:

$ pm2 start app.js --interpreter babel-node

回答by Naved Ahmad

Its very simple to resolve this issue, importis ES6 syntax and Node has difficulty in supporting it, you need to add Babelas a transpiler, go to package.json and add the following

解决这个问题很简单,import就是 ES6 语法,Node 难以支持,需要添加Babel作为转译器,到 package.json 中添加以下内容

First add a script tag to use babel while running the JS code for transpiling.

在运行 JS 代码进行转译时,首先添加一个 script 标签以使用 babel。

"scripts": {
    "start": "nodemon ./app.js --exec babel-node -e js"
  }

And then add the following as the Babel devDependencies

然后将以下内容添加为 Babel devDependencies

"devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1"
  }

after this you also need to configure the babel presets file, therefore create .babelrcfile at the root directory and define the presets as follows

之后你还需要配置 babel 预设文件,因此在根目录创建.babelrc文件并定义预设如下

{
  "presets": [
    "es2015",
    "stage-0"
  ]
}

Don't forget to do an npm installin the end

不要忘了做一个npm install到底

回答by Evan Carroll

Thanks to a NodeJS enhancement proposalwe have a path forward. You can use @standard-things/esm

多亏了NodeJS 增强提案,我们有了前进的道路。您可以使用@standard-things/esm

Find the announcement hereSimply run

这里找到公告只需运行

npm i --save @std/esm

In your packaged today.

在你今天打包。