简体   繁体   中英

Babel doesn't work in Node.js SyntaxError: Unexpected identifier

I'm hosting a nodejs project on server, that uses ES6 syntax, and I used babel-cli to compile it. I don't have any problem in my localhost, but when i host it I get this error:

import express from 'express';^^^^^^^SyntaxError: Unexpected identifier

my node version is v10.16.0

package.json :

 {
  "name": "example",
"version": "1.0.0",
 "description": "",
 "main": "index.js",
 "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
   "start": "nodemon ./index.js --exec babel-node -e js"
},
"author": "",
"license": "ISC",
"dependencies": {
 "bcrypt": "^3.0.2",
 "express": "^4.16.3",
 "jsonwebtoken": "^8.4.0",
"mongodb": "^3.1.6",
"mongoose": "^5.3.1",
"nodemailer": "^6.2.1",
"nodemon": "^1.18.4",
"randomstring": "^1.1.5",
"shortid": "^2.2.14"
 },
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-0": "^6.24.1"
 }
}

and for .babelrc :

      {
    "presets": [
    "env",
    "stage-0"
]

}

import express from 'express';
   ^^^^^^^

SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:721:23)
at Object.Module._extensions..js (internal/modules              /cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM