使用node构建的项目中默认是不支持import语法的,要使用babel去编译了才能执行,具体分为以下三步:
一、安装 babel-cli babel-preset-es2015
npm i babel-cli,babel-preset-es2015 -s
二、修改package.json
修改dev和start:--exec babel-node
{
...
"scripts": {
"dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server --exec babel-node",
"build": "nuxt build",
"start": "cross-env NODE_ENV=production node server/index.js --exec babel-node",
"generate": "nuxt generate"
},
"dependencies": {
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
...
},
"devDependencies": {
"nodemon": "^1.18.9"
}
}
三、根目录下新建.babelrc文件,内容如下:
{
"presets":["es2015"]
}