在线实时转换
需要babel-register
.babelrc中:
{
"presets": [
"es2015"
]
}
项目中main.js配置:
前提是安装对应的包
require('babel-register')
require('./src/app')
自己写的要运行的为app.js,这样配置后会在运行main.js是自动转为es5并执行
通过配置手动转换
需要babel-cli
安装babel后
运行babel src -d dist
src为自己写的es6目录文件,dist为转码后的es5文件,没有则创建
运行node dist/app.js
这样就可以以es5来运行自己的项目了
关于package.json中的配置
package.json:
{
"name": "edu",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"dev": "node main.js",
"build": "babel src -d dist",
"prestart": "npm run build",
"start": "node dist/app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bootstrap": "^3.3.7",
"echarts": "^3.4.0",
"ejs": "^2.5.5",
"express": "^4.14.1",
"font-awesome": "^4.7.0",
"jquery": "^3.1.1",
"nprogress": "^0.2.0"
},
"devDependencies": {
"babel-cli": "^6.22.2",
"babel-preset-es2015": "^6.22.0",
"babel-register": "^6.22.0"
}
}
dependencies
为项目依赖文件,上线必须要安装的
devDependencies
为开发阶段使用的工具,只在开发阶段使用
对配置的解释
"dev": "node main.js",
运行node run dev
=node main.js
"build": "babel src -d dist",
运行npm run build
=babel src -d dist
"prestart": "npm run build",
"start": "node dist/app.js"
运行npm start
或npm run start
=npm run build
+node dist/app.js