Search code examples
node.jsbabeljstranspiler

How to compile a file with babel CLI


I'm trying to compile a simple es6 file with babel CLI

Given the details below: what's going wrong?

$ node --version
v5.0.0

$ npm --version
3.3.6

$ npm init
$ npm install --save-dev babel-cli

$ echo -e 'import url from "url"\nconsole.log(`2+2=${2+2}`)' > script.js

$ ./node_modules/.bin/babel  script.js 
import url from "url";
console.log(`2+2=${ 2 + 2 }`);

In other words: I put in ES6 and I get out ES6 (albeit with slightly different spacing, and semicolons added). I'm expecting to see imports converted to requires, and to see my back-ticks disappear.

That is: I want ES5 out.

What do I need to do differently?


Solution

  • Babel version 6 ships "without any default transforms". You can read more about the changes in this blog post

    To transpile es6 to es5 you will need to do the following:

    1. run npm i --save-dev babel-preset-es2015

    2. Create a .babelrc file in the root of your project with the following:

      {

         "presets": ["es2015"]
      

      }

    There's a lot of configuration you can do on top of that, but that should atleast get you started.