refactor
refactor

Reputation: 15104

Unable to transpile es6 to es5 using Babel

I am using babel to transpile my es6 code to es5 in node application.

I have used below babel node modules for this

"babel-cli": "6.24.0"

"babel-preset-es2015": "6.24.0"

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

And below is related configuration in 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": "'[email protected]'>",
   "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

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.

Upvotes: 4

Views: 841

Answers (1)

Felix Kling
Felix Kling

Reputation: 817208

Babel doesn't find your configuration because you didn't setup package.json correctly. From the docs:

You can alternatively choose to specify your .babelrc config from within package.json like so:

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

Note that babel is at the top level, not inside config.

Upvotes: 3

Related Questions