“unexpected token import” in Nodejs5 and babel?

前端 未结 13 2060
清酒与你
清酒与你 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:27
    • install --> "npm i --save-dev babel-cli babel-preset-es2015 babel-preset-stage-0"

    next in package.json file add in scripts "start": "babel-node server.js"

        {
      "name": "node",
      "version": "1.0.0",
      "description": "",
      "main": "server.js",
      "dependencies": {
        "body-parser": "^1.18.2",
        "express": "^4.16.2",
        "lodash": "^4.17.4",
        "mongoose": "^5.0.1"
      },
      "devDependencies": {
        "babel-cli": "^6.26.0",
        "babel-preset-es2015": "^6.24.1",
        "babel-preset-stage-0": "^6.24.1"
      },
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "babel-node server.js"
      },
      "keywords": [],
      "author": "",
      "license": "ISC"
    }
    

    and create file for babel , in root ".babelrc"

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

    and run npm start in terminal

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