“unexpected token import” in Nodejs5 and babel?

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

    Current method is to use:

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

    And then in in .babelrc

    {
        "presets": ["env"]
    }
    

    this install Babel support for latest version of js (es2015 and beyond) Check out babeljs

    Do not forget to add babel-node to your scripts inside package.json use when running your js file as follows.

    "scripts": {
       "test": "mocha",
        //Add this line to your scripts
       "populate": "node_modules/babel-cli/bin/babel-node.js" 
    },
    

    Now you can npm populate yourfile.js inside terminal.

    If you are running windows and running error internal or external command not recognized, use node infront of the script as follow

    node node_modules/babel-cli/bin/babel-node.js

    Then npm run populate

    0 讨论(0)
  • 2020-11-30 19:19

    @jovi all you need to do is add .babelrc file like this:

    {
      "plugins": [
        "transform-strict-mode",
        "transform-es2015-modules-commonjs",
        "transform-es2015-spread",
        "transform-es2015-destructuring",
        "transform-es2015-parameters"
      ]
    }
    

    and install these plugins as devdependences with npm.

    then try babel-node ***.js again. hope this can help you.

    0 讨论(0)
  • 2020-11-30 19:20

    babel-preset-es2015 is now deprecated and you'll get a warning if you try to use Laurence's solution.

    To get this working with Babel 6.24.1+, use babel-preset-env instead:

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

    Then add env to your presets in your .babelrc:

    {
      "presets": ["env"]
    }
    

    See the Babel docs for more info.

    0 讨论(0)
  • 2020-11-30 19:21

    In your app, you must declare your require() modules, not using the 'import' keyword:

    const app = require("example_dependency");
    

    Then, create a .babelrc file:

    {
    "presets": [ 
        ["es2015", { "modules": false }]
    ]
    }
    

    Then, in your gulpfile, be sure to declare your require() modules:

    var gulp = require("gulp");
    
    0 讨论(0)
  • 2020-11-30 19:24

    Involve following steps to resolve the issue:

    1) Install the CLI and env preset

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

    2) Create a .babelrc file

    {
      "presets": ["env"]
    }
    

    3) configure npm start in package.json

    "scripts": {
        "start": "babel-node ./server/app.js",
        "test": "echo \"Error: no test specified\" && exit 1"
      }
    

    4) then start app

    $ npm start
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题