Search code examples
ecmascript-6babeljs

How do I get Babel 6 to compile to ES5 javascript?


I've installed the latest version of babel. Currently 6.4.0. I create a file called myclass.js that has the following code.

class MyClass {
    constructor(name) {
        console.log("I am a MyClass object .. ", name);
    }
}

var myclass = new MyClass('1234');

after creating my class I do the following in the command line.

$> babel ./src/myclass.js --out-file ./out/app.js

I would expect my app.js file to have es5 compiled javascript but it has the same exact code in it that the myclass.js file has. What am I missing that may be causing this?


Solution

  • The exact answer using babel with nodejs (and using CLI):

    Install babel and preset

    npm install babel-cli babel-preset-es2015
    

    Execute with npx

    npx babel  ./src/myclass.js --out-file ./out/app.js --presets babel-preset-es2015
    

    Note

    When babel-cli is globally installed could be not working. So this is my definitive solution