美文网首页
nodejs 支持 es6 & 自动重启

nodejs 支持 es6 & 自动重启

作者: 会煮咖啡的猫咪 | 来源:发表于2018-04-23 17:01 被阅读15次

    安装es6 依赖包

    npm i --save-dev babel-cli 
    npm i --save-dev babel-plugin-transform-async-generator-functions 
    npm i --save-dev babel-plugin-transform-object-rest-spread 
    npm i --save-dev babel-plugin-transform-runtime babel-preset-latest
    

    编写 .babelrc

    {
      "presets": ["latest"]
    }
    

    自动重启 依赖包

    npm i --save-dev nodemon
    

    编写 npm scripts

      "scripts": {
        "start": "nodemon ./src/server.js --exec babel-node -e js"
      },
    

    ./src/server.js 是你的 es6 代码

    执行

    npm start
    

    完整 package.json 参考

    {
      "name": "nodejs es6 run",
      "version": "0.1.0",
      "description": "",
      "main": "./src/server.js",
      "scripts": {
        "start": "nodemon ./src/server.js --exec babel-node -e js",
        "h" : "nodemon -h"
      },
      "author": "",
      "devDependencies": {
        "babel-cli": "^6.24.0",
        "babel-plugin-transform-async-generator-functions": "^6.24.1",
        "babel-plugin-transform-object-rest-spread": "^6.23.0",
        "babel-plugin-transform-runtime": "^6.23.0",
        "babel-preset-latest": "^6.23.0",
        "nodemon": "^1.11.0"
      },
      "dependencies": {
        "body-parser": "^1.17.1",
        "express": "^4.15.2",
        "lodash": "^4.17.4"
      }
    }
    

    相关文章

      网友评论

          本文标题:nodejs 支持 es6 & 自动重启

          本文链接:https://www.haomeiwen.com/subject/jdpllftx.html