文章來自於:https://www.codementor.io/iykyvic/writing-your-nodejs-apps-using-es6-6dh0edw2onode
第一步:建立項目文件夾並初始化Nodejs項目git
在須要建立項目的文件夾中,建立一個子文件夾,例如nodejs-es6,而後在根目錄下執行命令,只會生成package.json文件,也是最基礎的內容es6
npm init -y
第二步:建立index.js文件github
在根文件夾建立index.js文件,建立了文件,但未輸入文件內容express
第三步:安裝npm依賴組件npm
在根文件夾執行如下兩組命令,第一組是安裝express,未來咱們會用到,第二組是安裝babel等,在package.json中會用到json
npm install --save express
npm install --save-dev babel-cli babel-preset-es2015 rimraf
此時package.json的內容以下babel
{ "name": "nodejs-es6", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "express": "^4.15.3" }, "devDependencies": { "babel-cli": "^6.24.1", "babel-preset-es2015": "^6.24.1", "rimraf": "^2.6.1" } }
第四步:建立babel配置文件app
在根文件夾建立.babelrc文件,文件內容以下ide
{ "presets": ["es2015"] }
第五步:將babel配置加入至package.json
在package.json的scripts節點中加
"build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files", "start": "npm run build && node dist/index.js"
第六步:建立測試代碼
在index.js中輸入內容
import express from 'express'; const app = express() app.get('/', function (req, res) { res.send('Hello World!') }) app.listen(3000, function () { console.log('Example app listening on port 3000!') })
代碼 https://github.com/ChenWes/nodejs-es6-demo
參考babel官方的代碼:https://github.com/babel/example-node-server