首页 文章

无法使用Babel将es6转换为es5

提问于
浏览
3

我正在使用babel将我的es6代码转换为节点应用程序中的es5 .

为此,我使用了以下babel节点模块

“babel-cli”:“6.24.0”

“babel-preset-es2015”:“6.24.0”

“babel-preset-stage-2”:“6.22.0”

以下是package.json中的相关配置

{
   "name": "twinconsole",
   "version": "1.1.0",
   "description": "",
   "main": "dist/index.js",
   "scripts": {
       "test": "echo \"Error: no test specified\" && exit 1",
       "prebuild": "rimraf dist",
       "build": "babel --out-dir dist src"
   },
   "author": "'test@test.com'>",
   "license": "MIT",
   "devDependencies": {
      "babel-cli": "6.24.0",
      "babel-preset-es2015": "6.24.0",
      "babel-preset-stage-2": "6.22.0",
      "rimraf": "2.6.1"
  },
   "config": {
   "babel": {
       "presets": ["es2015" , "stage-2"]
    }
  }
}

我期待下面使用箭头功能的es6代码

module.exports.print = msg => {
    console.log(msg);
}

被转化为

module.exports.print = function(msg) {
    console.log(msg);
}

相反,转换后的代码仍然具有箭头功能 .

知道可能是什么问题 .

1 回答

  • 3

    Babel没有't find your configuration because you didn' t正确设置 package.json . 来自docs

    您也可以选择在package.json中指定.babelrc配置,如下所示:{
    “名字”:“我的包裹”,
    “版本”:“1.0.0”,
    “babel”:{
    //我的宝贝配置在这里
    }
    }

    请注意 babel 位于顶层,而不在 config 内 .

相关问题