New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require vs ES6 import #53

Closed
yarax opened this Issue Dec 18, 2015 · 33 comments

Comments

Projects
None yet
@yarax

yarax commented Dec 18, 2015

Are there any plans to move from CommonJs modules to ES6 modules and import syntax?

@bnoordhuis

This comment has been minimized.

Member

bnoordhuis commented Dec 18, 2015

You can find some discussion about require() vs. import here and there's more discussion scattered across various repositories.

In a nutshell, require() is not going anywhere - removing it would break too much for too little gain - but we'll almost certainly end up supporting ES6 import/export somehow, details TBD.

Support for ES6 modules first needs to land in V8. The tracking bug is here but it's blocked by finalization of the module loader spec.

@bnoordhuis bnoordhuis added the question label Dec 18, 2015

@yarax

This comment has been minimized.

yarax commented Dec 18, 2015

@bnoordhuis thanks!

@romu70

This comment has been minimized.

romu70 commented Jan 17, 2016

Hi,
Newbie question, if I write a ES6 class, like this one:

'use strict';

class User {
  constructor(username, email) {
    this.email = email;
    this.username = username;
  }
}

What's the correct syntax to export this class as a module? Tried many things export class..., module.exports = class..., export User as User, nothing seems to work, so I guess I make a mistake somewhere.

Thanks.

@bnoordhuis

This comment has been minimized.

Member

bnoordhuis commented Jan 18, 2016

@romu70 import/export isn't supported yet but module.exports = ... should work:

// a.js
class A {
  // ...
}
module.exports = A;

Or:

// b.js
module.exports = class B {
  // ...
};
@StyMaar

This comment has been minimized.

StyMaar commented Mar 23, 2016

Support for ES6 modules first needs to land in V8.

Why ?

The current module syntax doesn't rely on any V8 support to work and import/export can be transpilled to this model. Node's concerns about module loading and browsers' ones are quite different.
I don't really understand how the whatwg module loader spec should affect Node, as long as the import/export semantic is already specified.

@bnoordhuis

This comment has been minimized.

Member

bnoordhuis commented Mar 23, 2016

Is your question "why won't node.js support import/export until V8 does"? Because node.js doesn't parse JS code, V8 does, and we're not a transpiler.

@StyMaar

This comment has been minimized.

StyMaar commented Mar 23, 2016

Fair enough, thanks.

@paulwalker

This comment has been minimized.

paulwalker commented Apr 30, 2016

Does v5+ of V8 (and consequently nodejs v6+) support es6 modules yet?

@bnoordhuis

This comment has been minimized.

Member

bnoordhuis commented May 1, 2016

@paulwalker No, not yet. Maybe in node.js v7.

@styfle

This comment has been minimized.

styfle commented Jun 18, 2016

You can star v8 Bug 1569.

@franciscop

This comment has been minimized.

franciscop commented Aug 29, 2016

@lordKnighton why was this issue closed? I'd love to see this implemented as well

@getkey

This comment has been minimized.

getkey commented Aug 29, 2016

@franciscop

Support for ES6 modules first needs to land in V8. The tracking bug is here but it's blocked by finalization of the module loader spec.

You can star v8 Bug 1569.

@SamMorrowDrums

This comment has been minimized.

SamMorrowDrums commented Dec 5, 2016

Just came back here for an update. Currently there is blocking bug. https://bugs.chromium.org/p/v8/issues/detail?id=5685

Really looking forward to this finally dropping, it is so close, and the last thing that's preventing me dropping babel on some projects.

AndreasAbdi added a commit to AndreasAbdi/randomDistrib-backend that referenced this issue Dec 6, 2016

Added basic express server.
- uses commonjs module loading cuz node is bugged atm. 
- nodejs/help#53
@mayeaux

This comment has been minimized.

mayeaux commented Jan 10, 2017

I've been reading up on ES Modules being supported in native node, it looks like there's two simultaneous discussions of how to implement it (ie, whether to use the .mjs file extension) and then also a consideration around a bug in V8.

Which is the true current blocker? Does anyone have an ETA on when the blocker should be smoothed out and V8 will support the ES Module syntax? I'm in the same boat as a few others here who are hoping to drop babel, thanks!

@mweels

This comment has been minimized.

mweels commented Jan 14, 2017

Why would they support it when you could use transcompilers?

@dandv

This comment has been minimized.

dandv commented Jan 15, 2017

Since 2017-Jan-10, V8 supports modules.

@mayeaux

This comment has been minimized.

mayeaux commented Jan 16, 2017

Awesome! And it looks like nightly node (and 7.4) uses v8 version: 5.4.500.46

Whereas the latest V8 version with es modules landed is around: 5.7.458

Wonder how long it will take to land in node 🤔 I don't actively develop node do any devs have a guesstimate?

@MylesBorins

This comment has been minimized.

Member

MylesBorins commented Jan 16, 2017

@mrmayfield it should eventually be on this experimental branch

fwiw it landing in V8 will be only the first step towards proper support in node. I believe we are currently tracking V8 5.5 for inclusion in node 8... but other people will know better.

/cc @nodejs/v8

@mayeaux

This comment has been minimized.

mayeaux commented Jan 16, 2017

Thanks @MylesBorins
So there's still determinations to be made in regards to how to implement the actual feature even though it's now landed in V8? Sorry I'm a bit unfamiliar with the intricacies of how Node/V8 interact.

@targos

This comment has been minimized.

Member

targos commented Jan 16, 2017

@mrmayfield yes. It is happening here: nodejs/node-eps#39

@dandv

This comment has been minimized.

dandv commented Feb 19, 2017

From James M Snell's Update on ES6 Modules in Node.js:

Work is in progress but it is going to take some time — We’re currently looking at around a year at least.

@mk-pmb

This comment has been minimized.

mk-pmb commented Dec 19, 2017

How's progress so far?

@srameshr

This comment has been minimized.

srameshr commented Jan 22, 2018

On node 8.x, its not supported yet.

@caonUlisses

This comment has been minimized.

caonUlisses commented Mar 4, 2018

You can use it with for .mjs files with the --experimental-modules flag, as had being pointed already, it's experimental, but it's already happening.

@cztomsik

This comment has been minimized.

cztomsik commented Mar 13, 2018

Currently, it's not possible to import the same file from chrome and node.js at the same time. Something should be done about this.

@devsnek

This comment has been minimized.

Member

devsnek commented Mar 13, 2018

@cztomsik

Currently, it's not possible to import the same file from chrome and node.js at the same time. Something should be done about this.

i'm not sure what exactly is lacking? obviously browsers won't implement node's loader algorithm or builtin modules. there is discussion around allowing node to import from urls but there's a lot of work to do behind that.

@cztomsik

This comment has been minimized.

cztomsik commented Mar 14, 2018

@StyMaar

This comment has been minimized.

StyMaar commented Mar 14, 2018

application/javascript is a MIME type set by the web-server in the HTTP response header.
Most http server automatically set the application/javascript MIME header when delivering files with a .js extension but you can configure your web server to do the same for .mjs.

@cztomsik

This comment has been minimized.

cztomsik commented Mar 14, 2018

@StyMaar

This comment has been minimized.

StyMaar commented Mar 14, 2018

What's your use-case ?

In general, if you can't configure the MIME type, and the .mjs extension is misbehaving on the server, then the bug is on the webserver's / hosting service side and should be filed there.

I'm still curious about your specific case though.

@devsnek

This comment has been minimized.

Member

devsnek commented Mar 14, 2018

for those of you following along at home: https://datatracker.ietf.org/submit/status/88872/

@styfle

This comment has been minimized.

styfle commented Mar 16, 2018

Currently, it's not possible to import the same file from chrome and node.js at the same time. Something should be done about this.

@cztomsik There is something being done about it on the web side of things too.
See https://github.com/domenic/package-name-maps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment