import express from 'express'; ^^^^^^^ SyntaxError: Unexpected identifier at Module._com

import express from 'express'; ^^^^^^^ SyntaxError: Unexpected identifier at Module._com_第1张图片
安装express时遇到这个问题,原因是express不支持ES6语法,需要安装babel

命令

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

安装完之后创建 .babelrc 文件

import express from 'express'; ^^^^^^^ SyntaxError: Unexpected identifier at Module._com_第2张图片

创建完之后加入

{
    "presets" : ["env"]
}

修改package.json文件

修改这一处

import express from 'express'; ^^^^^^^ SyntaxError: Unexpected identifier at Module._com_第3张图片

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon --exec babel-node -- ./server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "nodemon": "^1.19.1"
  }
}

npm run start

你可能感兴趣的:(import express from 'express'; ^^^^^^^ SyntaxError: Unexpected identifier at Module._com)