Babel在Node.js语法错误中不起作用:意外的标识符

时间:2019-07-31 18:23:49

标签: javascript node.js ecmascript-6 babeljs es6-modules

我正在服务器上托管一个nodejs项目,该项目使用ES6语法,并且使用babel-cli对其进行编译。我的本地主机没有任何问题,但是当我托管它时,出现此错误:

  

从'express'导入express; ^^^^^^^^ SyntaxError:意外标识符

我的节点版本为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"
 }
}

.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)

1 个答案:

答案 0 :(得分:2)