“unexpected token import” in Nodejs5 and babel?

前端 未结 13 2125
清酒与你
清酒与你 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:25

    You have to use babel-preset-env and nodemon for hot-reload.

    Then create .babelrc file with below content:

    {
      "presets": ["env"]
    }
    

    Finally, create script in package.json:

    "scripts": {
        "babel-node": "babel-node --presets=env",
        "start": "nodemon --exec npm run babel-node -- ./index.js",
        "build": "babel src -d dist"
      }
    

    Or just use this boilerplate:

    Boilerplate: node-es6

提交回复
热议问题