Settings

Theme

Using ES6 with npm today

mammal.io

23 points by jallardice 11 years ago · 7 comments

Reader

latchkey 11 years ago

Wow, I wish this had been posted a few days ago when I was going through the same bewilderment. There doesn't seem to be very many ES6 projects on npm, so examples are few and it took a bit of trial and error to get it right. I basically ended up with a very similar set of code for my project... https://github.com/lookfirst/gulp-helpers/

arcatek 11 years ago

The last part is missing : how should non-ES6 Node scripts require the ES6 components ?

  • usagimaru 11 years ago

    The transpilers will compile the module into an object you can require normally. The default export is under { "default": ... } and the other exported properties are under their exported names.

    The real fun part is going the other direction: using non-ES6 modules inside ES6 code. Using 6to5ify (now babelify), I had the issue that I couldn't access the `exports` variable, only properties on it.

    • jallardiceOP 11 years ago

      I've not had any problems with importing non-ES6 modules into ES6 code. To use Express for example you can simply do `import express from 'express'`. If you only need access to one or two properties of the exported object you can use the destructuring syntax to just get references to them: `import { hash, compare } from 'bcrypt'`

  • jallardiceOP 11 years ago

    You're right, this would have been a good final section. However it's pretty straightforward as Babel compiles `export` syntax to Node's `module.exports` format by default which means you can require them in the usual way. It also compiles `import` syntax to the normal `require` calls.

  • lenkite 11 years ago

    They should simply use standard node js require. The author forgot to specify common js interop option in babel.

    babel --modules common -d lib/ src/

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection