4

I just download Babel using npm (npm install -g babel-cli), but when I use babel-cli to compile code, stdout just returns an identical copy of the uncompiled code I tried to compile.

My babel version is 6.1.18 (babel-core: 6.1.21), and node version is 4.2.2.

I tested both OS X 10.9 and OS X 10.10, they have the same problem.

Bebel does not even work with the code below when I use babel test.js:

let a = '';
class b {

}

enter image description here

Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
Zizy
  • 191
  • 1
  • 1
  • 7

2 Answers2

2

From their website:

Babel 6 ships without any default transforms, so when you run Babel on a file it will just print it back out to you without changing anything.

run this command and install the es2015 presets first:

npm install --save-dev babel-preset-es2015

now compile with:

babel test.js --presets es2015

I also noticed that you have to do --save-dev when you npm install babel-preset-es2015 and that if you just use -g you get the error Error: Couldn't find preset "es2015" so make sure to do --save-dev and not -g when installing the preset.

David Zorychta
  • 13,039
  • 6
  • 45
  • 81
2

As of version 6, Babel does not ship with any transformation by default. So, babel-preset-es2015 must be installed to enable the transformations. After installation, you have to enable the preset either with a package.json or with the .babelrc file. A sample package.json file:

{
  "name": "es6-test",
  "version": "1.0.0",
  "babel": {
    "presets": ["es2015"]
  }
}

And if you use .babelrc instead:

{
  "presets": ["es2015"]
}

This article might help with detailed instructions.

On a related note, if you install the preset globally with -g or --global, make sure to make a symlink in your project directory like this npm link babel-preset-es2015.

Ivey
  • 499
  • 4
  • 13