当前位置: 首页 > 工具软件 > express-babel > 使用案例 >

express-generator脚手架,引入babel,支持import和async,绝对可用

皇甫飞光
2023-12-01

1.首先在package.js中加入如下的依赖包,并需要修改启动的路径(后面有说要自己新增个index.js文件)

{
  "name": "express-app",
  "version": "0.0.0",
  "private": true,
  "scripts": {
  // 需要改变的启动路径
    "start": "node ./index.js",
    "devstart": "nodemon ./index.js"
  },
  "dependencies": {
   // 加入的依赖包,到时去掉这段话
    "babel": "^6.23.0",
    "babel-cli": "^6.24.1",
    "babel-core": "^6.24.0",
    "babel-preset-es2015": "^6.24.0",
    "babel-preset-stage-3": "^6.22.0",
    "babel-register": "^6.24.0"
  },
  "devDependencies": {
   // 加入的依赖包,到时去掉这段话
    "babel-plugin-transform-async-to-generator": "^6.24.1",
    "babel-plugin-transform-es2015-classes": "^6.24.1",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
    "babel-plugin-transform-export-extensions": "^6.22.0"
  }
}

2.执行yarn或者npm i命令更新依赖包

3.在根目录新建.babelrc,加入如下代码

{
  	"presets": ["stage-3"],
  	"plugins": [
  		"transform-async-to-generator",
        "transform-es2015-modules-commonjs",
        "transform-export-extensions"
    ]
}

4.在根目录新建index.js,加入如下代码

require('babel-core/register');
require('./bin/www');

5.然后再执行npm run devstart启动命令就可以了

亲测绝对可用,如果遇到问题,可直接留言我.

 类似资料: