节点错误:SyntaxError:意外的令牌导入 [英] Node error: SyntaxError: Unexpected token import

查看:102
本文介绍了节点错误:SyntaxError:意外的令牌导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这是怎么回事.我在其他论坛上讨论了翻译和通天塔.我该怎么办?

node -v
v5.5.0

我的代码:

import recast from 'recastai'

和错误

(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

解决方案

ES6导入是最近引入的功能,并且Node的当前稳定版本尚不支持它们.为此,Node.js问题跟踪器有一个公开问题-但直到V8和Node添加支持要使用此功能,您将需要使用转译器(最受欢迎的是 babel )才能使用导入. /p>

为了快速尝试转译,babel提供了基于Web的REPL. babel项目主页指向与Babel入门并将其与您的开发工作流程集成的相关资源.

要进行最简单的设置,请访问设置页面,然后在Babel内置的ins部分.

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

  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

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

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

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

解决方案

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 issue for 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.

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

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 page and select CLI in the Babel built-ins section.

This basically involves three simple steps:

  1. Install babel-cli : npm install --save-dev babel-cli babel-preset-es2015
  2. Create .babelrc configuration file: echo '{ "presets": ["es2015"] }' > .babelrc
  3. Use the installed module to transpile your source code: ./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.

这篇关于节点错误:SyntaxError:意外的令牌导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆