简体   繁体   English

无法使用Babel将es6转换为es5

[英]Unable to transpile es6 to es5 using Babel

I am using babel to transpile my es6 code to es5 in node application. 我正在使用babel将我的es6代码转换为节点应用程序中的es5。

I have used below babel node modules for this 为此,我使用了以下babel节点模块

"babel-cli": "6.24.0" “babel-cli”:“6.24.0”

"babel-preset-es2015": "6.24.0" “babel-preset-es2015”:“6.24.0”

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

And below is related configuration in package.json 以下是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"]
    }
  }
}

I was expecting below es6 code which uses Arrow function 我期待下面使用箭头功能的es6代码

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

to be transpiled to 被转化为

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

Instead the transpiled code still has arrow function. 相反,转换后的代码仍然具有箭头功能。

Any idea what could be the issue. 知道可能是什么问题。

Babel doesn't find your configuration because you didn't setup package.json correctly. Babel找不到您的配置,因为您没有正确设置package.json From the docs : 来自文档

You can alternatively choose to specify your .babelrc config from within package.json like so: 您也可以选择在package.json中指定.babelrc配置,如下所示:

 { "name": "my-package", "version": "1.0.0", "babel": { // my babel config here } } 

Note that babel is at the top level, not inside config . 请注意, babel位于顶层,而不是config内部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM