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

express-generator 中使用babel import

夏季萌
2023-12-01

在package.json中

"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",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.9",
    "express": "~4.16.0",
    "http-errors": "~1.6.2",
    "jade": "~1.11.0",
    "morgan": "~1.9.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"
  }

在根目录建立文件夹.babelrc

内容为

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

启动项目的命令为 npm start 执行的文件路径为 node ./bin/www.js

现在我们在根目录建立index.js 内容为

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

然后修改package.json 

"start": "node ./index.js"

这样我们在执行命令 npm start 的时候就会走index,js文件 从而运行了babel 将es6转化为了es5

 类似资料: