Node.js中使用ES6
ES6有很多有用的功能,比如说异步的操作,但是很多高级功能node是不支持的,就需要使用babel转换成ES5
(1)babel转换配置,项目根目录添加.babelrc 文件
{
"presets": [
"stage-3",
"es2015"
]
}
(2)安装es6转换模块
cnpm install babel-preset-es2015 --save-dev
(3)全局安装命令行工具
cnpm install babel-cli -g
(4)使用
babel-node js文件名
(5)在package.json文件中添加指令:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"babel-core": "^6.26.3",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-3": "^6.24.1",
"babel-runtime": "^6.26.0",
"bluebird": "^3.5.4",
"body-parser": "^1.18.3",
"cookie-parser": "^1.4.4",
"ejs": "^2.6.1",
"express": "^4.16.4",
"multer": "^1.4.1",
"mysql": "^2.16.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"rimraf": "^2.6.3"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",
"start": "babel-node ./app.js"
},
"author": "",
"license": "ISC"
}