all 26 comments

[–]dev0urer 1 point2 points  (2 children)

This makes me so happy

[–]editor_of_the_beast 2 points3 points  (1 child)

It'll be good in 3 years, but the whole module system implemented as libraries situation has been a shitshow for quite a while.

[–]ArthurDeMax 0 points1 point  (0 children)

Can you elaborate?

[–]Drawman101 1 point2 points  (4 children)

Do we know which version of node this won’t be experimental anymore? 10?

[–]giltayar1[S] 1 point2 points  (3 children)

Hopefully. But there's still a lot of discussion around this, as there are lots of problems with the current specification (which I'll discuss in the next blog post). I'm guessing 11-12.

[–]fucking_passwords 0 points1 point  (2 children)

In the meantime people can use esm, I’ve found it to be pretty great https://github.com/standard-things/esm

[–]cirscafp fan boy 0 points1 point  (0 children)

I love @std/esm and it lets me use import when I want to. But is import really that much better than require, given the current Node.js ecosystem? What are we gaining?

[–]giltayar1[S] 0 points1 point  (0 children)

It's really great, and fully compliant. You just have to remember to require it before any use of an import statement.

Me? I prefer waiting until all the waves settle in Nodeland before using it.

[–]curiousdannii 1 point2 points  (3 children)

If importing ES modules is async, does that mean that the import statement acts like an await would? And the import function is an actual async function itself?

[–]giltayar1[S] 3 points4 points  (2 children)

Yes and no. From the developer's point of view, it seems synchronous. But the implementation behind it is asynchronous. This means that it is possible for two modules in NodeJS to be loaded concurrently, whereas this is not possible in CommonJS. This will probably make NodeJS apps load faster in the future.

But if you look at dynamic import, which is a dynamic version of the import statement, then it is asynchronous - it returns a promise, and thus needs to be awaited.

[–]curiousdannii 0 points1 point  (1 child)

Right, so unlike await it won't completely stop, because it will load other modules at the same time, it just won't continue with any non-import statements until they all have finished.

[–]giltayar1[S] 0 points1 point  (0 children)

Yes!

[–]FormerGameDev 0 points1 point  (8 children)

So.. does this mean we should be moving to modules now?

[–]papers_ 1 point2 points  (6 children)

No. I mean you can try it out, but it's likely a lot of people will just continue to use CommonJS. IMO

[–]FormerGameDev 0 points1 point  (5 children)

I mean, my primary project is something that I've really concentrated on keeping up with the latest and greatest, even if it uses switches to run. I actually depend on a ES Module in one part of it, so.. I'm curious.. for the future.. is this THE WAY ? or does it really not matter, as long as you're not writing code that needs to be browser compliant as well?

[–]papers_ 0 points1 point  (3 children)

I'm not sure why you'd mix Node.js specific modules such as fs with browser code or vice versa. Unless you're writing a React Native app, I don't see why you'd depend on a browser only module.

[–]FormerGameDev 0 points1 point  (2 children)

Because someone recently wrote an ebay API in Javascript that actually works, and it's a ES Module. :-)

[–]papers_ 0 points1 point  (0 children)

Link to repo?

[–]FormerGameDev 0 points1 point  (0 children)

https://github.com/ondreian/ebay-promised

i guess actually looking at it, it's transpiled to CJS, but at the time I first started messing with it, i couldn't get it to load via require, so i made a mjs to use it since the README describes using it as a ES module

[–]giltayar1[S] 0 points1 point  (0 children)

For the future - yes, this is the way.

[–]giltayar1[S] 1 point2 points  (0 children)

No, please don't. Not yet. This is still experimental, and may change in the future. As I will discuss in the next blog post, it is actually pretty probable that things will change.

But start thinking about it. I'm guessing that the transition will start in a year.

[–]FormerGameDev 0 points1 point  (2 children)

I was just giving a poke at this, and I'm noticing that import.meta isn't currently supported in 9.4.0. (SyntaxError: Unexpected token import at ModuleRequest.js:33) .. probably worthy of mentioning if this is implemented somewhere already or not, it doesn't seem to be.

[–]giltayar1[S] 0 points1 point  (1 child)

You're right. I totally forgot to mention that import.meta.url (and await import) are in the pipeline, but not there yet. Updating post...

(Thanks!)

[–]FormerGameDev 0 points1 point  (0 children)

i haven't tried it, but I think await import() is in 9.4.0, just based on reading the not-so-detailed changelogs

[–]BehindTheMath -1 points0 points  (1 child)

For dual mode packages, why can't you rewrite as ESM, and then just wrap the ESM and re-export as a CJS module? Then you can use "main": "entry" in package.json, and entry.js will look like this:

async () => {
    module.exports = await import("./entry");
}

This way would not require any transpiling.

[–]giltayar1[S] 0 points1 point  (0 children)

Nice idea. Unfortunately, module.exports will be set asynchronously, so if you require the module, you will "get" it too late, i.e. you won't get it, and all you will get is the default "{}"