“unexpected token import” in Nodejs5 and babel?

前端 未结 13 2142
清酒与你
清酒与你 2020-11-30 18:40

In js file, i used import to instead of require

import co from \'co\';

And tried to run it directly by nodejs since it said import is \'shi

13条回答
  •  心在旅途
    2020-11-30 19:16

    Until modules are implemented you can use the Babel "transpiler" to run your code:

    npm install --save babel-cli babel-preset-node6
    

    and then

    ./node_modules/babel-cli/bin/babel-node.js --presets node6 ./your_script.js
    

    If you dont want to type --presets node6 you can save it .babelrc file by:

    {
      "presets": [
        "node6"
      ]
    }
    

    See https://www.npmjs.com/package/babel-preset-node6 and https://babeljs.io/docs/usage/cli/

提交回复
热议问题