es6
file with babel CLIGiven 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.
Simply add a "scripts" field to your package. json and put the babel command inside there as build . This will run Babel the same way as before and the output will be present in lib directory, only now we are using a local copy. Alternatively, you can reference the babel cli inside of node_modules .
Babel comes with a built-in CLI which can be used to compile files from the command line. In addition, various entry point scripts live in the top-level package at @babel/cli/bin . There is a shell-executable utility script, babel-external-helpers.
You can also check the version of babel-cli by finding the babel-cli folder in node_modules and looking at the version property of the package. json that is at the base of that folder. If babel-cli was installed globally via -g flag of npm install , you could check the version by executing command babel --version .
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:
run npm i --save-dev babel-preset-es2015
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.
The trick: need to pass --presets "es2015"
as an argument to babel.
As in:
$ ./node_modules/.bin/babel --presets "es2015" script.js
"use strict";
var _url = require("url");
var _url2 = _interopRequireDefault(_url);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
console.log("2+2=" + (2 + 2));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With