Skip to content
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
Closed

require vs ES6 import #53

yarax opened this issue Dec 18, 2015 · 33 comments
Labels

Comments

@yarax
Copy link

yarax commented Dec 18, 2015

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

@bnoordhuis
Copy link
Member

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.

@yarax
Copy link
Author

yarax commented Dec 18, 2015

@bnoordhuis thanks!

@romu70
Copy link

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
Copy link
Member

@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
Copy link

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
Copy link
Member

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
Copy link

StyMaar commented Mar 23, 2016

Fair enough, thanks.

@paulwalker
Copy link

paulwalker commented Apr 30, 2016

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

@bnoordhuis
Copy link
Member

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

@styfle
Copy link
Member

styfle commented Jun 18, 2016

You can star v8 Bug 1569.

@franciscop
Copy link

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

@getkey
Copy link

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
Copy link

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 pushed a commit to AndreasAbdi/randomDistrib-backend that referenced this issue Dec 6, 2016
- uses commonjs module loading cuz node is bugged atm. 
- nodejs/help#53
@xgqfrms-GitHub
Copy link

xgqfrms-GitHub commented Dec 29, 2016

https://github.com/nodejs/node/wiki/ES6-Module-Detection-in-Node
http://stackoverflow.com/questions/37132031/nodejs-plans-to-support-import-export-es6-es2015-modules
http://stackoverflow.com/questions/31354559/using-node-js-require-vs-es6-import-export

https://nodejs.org/en/docs/es6/

@mayeaux
Copy link

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
Copy link

mweels commented Jan 14, 2017

Why would they support it when you could use transcompilers?

@dandv
Copy link

dandv commented Jan 15, 2017

Since 2017-Jan-10, V8 supports modules.

@mayeaux
Copy link

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
Copy link

@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
Copy link

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
Copy link
Member

targos commented Jan 16, 2017

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

@dandv
Copy link

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
Copy link

mk-pmb commented Dec 19, 2017

How's progress so far?

@srameshr
Copy link

On node 8.x, its not supported yet.

@caonUlisses
Copy link

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
Copy link

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
Copy link
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
Copy link

cztomsik commented Mar 14, 2018 via email

@StyMaar
Copy link

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
Copy link

cztomsik commented Mar 14, 2018 via email

@StyMaar
Copy link

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
Copy link
Member

devsnek commented Mar 14, 2018

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

@styfle
Copy link
Member

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
Labels
Projects
None yet
Development

No branches or pull requests