James M. Lay James M. Lay - 2 months ago 7
Node.js Question

Easiest way to experiment with `import` ES6

The recent Meteor tutorials use the

import
aspect of ES6 a lot, and I'd like to know if there's an es6 wrapper or other simple way to experiment with this specific feature without having to create a new
package.json
,
npm install babel --save
,
npm start
, etc...

Ideally, something like this:

> echo 'import "./importme.js";' > new.js
> echo 'console.log('hi');' > import.js
> es6 new.js
hi
>


I did notice babel-node, but it's complaining that import is not a valid token, and apparently it doesn't support
import
from the REPL.

Is there a REPL or node wrapper that supports
import
?

Thanks

Answer

babel-node will work fine, but babel doesn't do anything out of the box since version 6. You have to install presets to opt-in to the new features.

  1. npm install babel-node
  2. npm install babel-preset-es2015 Installs the preset for ES2015 syntax.
  3. touch .babelrc Create a .babelrc file to tell babel which presets you are using

Inside your .babelrc file, add the following code:

{
  "presets": ["es2015"]
}

Now, running babel-node will work.