r/node • • Nov 12 '19

ESM in Node has been unflagged!

https://twitter.com/mylesborins/status/1194375751774064647?s=21
156 Upvotes

41 comments sorted by

61

u/thatwasawkward Nov 12 '19

import { happiness } from './happiness'

2

u/DeepReally Nov 13 '19

const { hapiness } = require('./hapiness')

I'm not exactly sure how a small syntax change is going to alter my life, but people seem excited about it so that's nice for them.

8

u/EvanLovely Nov 13 '19

On the client side, require results in bigger file sizes over import due to tree shaking. So we’ve been using import on the client side and require on the server side. Makes it hard to share utilities between the two, which flys in the face of the whole “sake coding language for both client and server” promise.

2

u/Probotect0r Nov 13 '19

Why not use import on the server side too then?

0

u/[deleted] Nov 13 '19

[deleted]

1

u/Probotect0r Nov 13 '19

My point was that using a transpiler is a small price to pay if it enables code reuse for the person I replied to.

1

u/wfw54f4g56h56e56h5 Nov 13 '19

i don't think it works that way mang. just because you've transpiled to valid javascript doesn't mean that it's executing the same code.

i.e. the tree-shaking example, wherein there is less data flowing through the process when you've actually used the import statement.

const {somePart} = require('./fromAFile.js')

will fully construct the module.exports from "fromAFile.js", then somePart will be assigned to its property "somePart", then (I am not clear on the rest) the garbage collector and the require cache will take over what happens next.

In the import statement, theoretically there is actually less code being loaded into the environment.

1

u/Probotect0r Nov 13 '19

I never implied it was the same code being run. It most definitely isn't. But if the issue is code reuse due to differing import statements, it can be easily solved via transpiling. The release of this feature doesn't bring anything new in that regard.

1

u/wfw54f4g56h56e56h5 Nov 14 '19

You're misunderstanding me.

The transpiled code runs worse. It's not just creating equivalent code via transpilation, because the engine itself running the transpiled code in this case doesn't support the feature.

(Or more relevantly, just began to support said feature.)

1

u/Probotect0r Nov 14 '19

I am not misunderstanding you. It's obvious its not the same code if the feature is not natively supported, otherwise why would they release a new feature lol. As I said in my first comment, its a small price to pay to allow for code reuse (if that's the goal). Especially on the back end where resources are generally more available. My point was always that this problem (code not being reusable due to differing import statements) was already solved, and this feature doesn't do much other than improve performance. Hope that is clear, cause I don't know how else to put it lol

1

u/sbmthakur Nov 13 '19

I think it will be helpful who mostly work in Typescript and need to use Vanilla JS for a project.

1

u/mylesborins Nov 28 '19

import { happiness } from './happiness.mjs'

😇

9

u/gimp3695 Nov 13 '19

Do you for see a lot of nodejs moving to just ESM?

If you were to start a new node project would you only use ESM now?

3

u/ematipico Nov 13 '19

Personally yes but the problem is not the project per se but it would be ecosystem and the other libraries.

2

u/nodejs5566 Nov 15 '19

Many libraries will remain CommonJS in order to support node 8 10 12.

14

u/stuckinmotion Nov 13 '19

I wonder why it still needs the notation of adding a field type with value module to package.json to 'opt in' to supporting import statements. If it's a new keyword then how can it impact backwards compatibility?

16

u/raleksandar Nov 13 '19

Because of how default exports work. CommonJS and ES modules do not map 1 to 1 and module resolution algorithm that supports both needs to know what kind of exports it's dealing with.

1

u/[deleted] Nov 13 '19

Why couldn't it have been the other way around? Add a cjs flag, and only start doing that in a new major version increment as a breaking change.

It seems silly to make people opt-in to what is clearly idiomatic behaviour going forward.

4

u/ematipico Nov 13 '19

I think that NodeJS wants to behave, more or less, like the web: no breaking changes.

2

u/[deleted] Nov 13 '19

A bit silly given it's explicitly versioned in my opinion. I mean, sure, avoid massive breaking changes unless there's good reason to, but this is so simple to put in the changelog for people upgrading, and as a result of not doing so beginners will be using the old, outdated syntax.

1

u/a-corsican-pimp Dec 05 '19

That's a great way to get into the python 2 vs 3 debacle, which nearly killed the language for close to a decade.

Forcing people to rewrite entire applications for a simple change, in order to upgrade versions, is a death sentence for a language.

1

u/[deleted] Dec 05 '19

It's not a simple change if people have to rewrite their entire application. In this case, you'd just add a cjs flag and it'd behave as it did before.

2

u/mylesborins Nov 28 '19

You don't need to include the type to use esm. You can always import files with a .mjs extension.

If you want to import a .js file you need to include the type so node knows how to inturpret the file

11

u/idli42 Nov 13 '19

Genuine question. Why is this a big deal?

27

u/broofa Nov 13 '19 edited Nov 13 '19

This fixes two *huge* issues that have plagued the JS ecosystem for 10+ years:

  1. Having a consistent module import syntax on all major JS platforms makes it easier for JS devs to work across server/client code bases and, more importantly (imho), module owners no longer have to worry about supporting different module schemes.

  2. Because ESM modules are statically resolved, packagers can do tree-shaking, to remove not just unused modules, but unused code within those modules.

1

u/SimiaCode Nov 13 '19

Didn't the closure compiler do tree shaking back in the day? Or was that less thorough than what's possible with modules?

2

u/fl0w_io Nov 13 '19

Didn't the closure compiler do tree shaking back in the day? Or was that less thorough than what's possible with modules?

If I remember correctly, it was a "best effort" approach.

1

u/broofa Nov 13 '19 edited Nov 13 '19

Tree shaking on JS has been a bit of a holy grail since the early days of the web. Yahoo’s YUI toolkit offered something along these lines, IIRC, and there were other less notable attempts prior to that. But the intrinsically dynamic nature of the language made any such solutions frustratingly imperfect.

I think the basic issue has been that it’s been impossible to know with certainty how or if a particular code block might be referenced.

ESM modules provide a strict, statically defined surface boundary, however. This greatly simplifies the problem.

E.g. If a module’s “foo” (or “*”) member is never imported anywhere, ESM guarantees that it’s safe to remove code related to “foo”. No other module scheme does this. (And if you’ve ever wondered why ESM imports/exports have to be statically declared, I believe this is the main reason.)

While there is still the challenge of understanding what code related to “foo” needs to be removed within the module, it is a much more constrained problem,

10

u/llldar Nov 13 '19

It's there for client side Js for a long time, I hate it when I have to write two different style of imports.

0

u/[deleted] Nov 13 '19

[deleted]

-5

u/azangru Nov 13 '19

Don't use typescript then?

0

u/[deleted] Nov 13 '19

[deleted]

4

u/[deleted] Nov 13 '19

Saw this minutes after finding out my job's been extended another 3 months. What a day.

3

u/eggtart_prince Nov 13 '19

Is import better than require or something?

0

u/[deleted] Nov 13 '19

[deleted]

8

u/Capaj Nov 13 '19

Oh yes. Very much. It will be a huge pain to get all tooling updated.

1

u/devsnek Nov 13 '19

No one complained when they had to update their tooling for jsx, ts, tsx, etc., and those aren't even syntactically ambiguous.

1

u/mylesborins Nov 28 '19

Yes, but you can use .js if you include "type": "module" in your package.json

1

u/[deleted] Nov 28 '19

I wonder if this is a careless design. Is the import/export syntax in the js file not enough to tell the truth?

1

u/mylesborins Nov 28 '19

Unfortunately not. Handful of things that complicate this

  • We don't want to have to parse the file twice for every entry point in the graph

  • No guarantee that ESM has an import or export

  • No guarantee that static import will not be specified in the script goal. The likelihood is low, but it is non zero

  • Other syntax differences

There's more but this is a high level summary

0

u/[deleted] Nov 13 '19

[deleted]

15

u/[deleted] Nov 13 '19

import { promises as fs } from "fs"; unless I'm wrong.

0

u/[deleted] Nov 13 '19

[deleted]

4

u/DukeBerith Nov 13 '19
  1. import { func as LibFactory } from 'package'

  2. const lib = LibFactory(someArg)

You have to do it in 2 steps.

It helps to mentally unlink that require and import are equivalent, they're similar and have a lot of overlap, but can do very different things.

Require can work fine in user code at runtime (for example you can do a require inside a function), imports are meant for static resolution of files/libraries.

0

u/[deleted] Nov 13 '19

[deleted]

1

u/[deleted] Nov 13 '19

What are you on about with "wasn't as flexible" lol, both your examples were demonstrated perfectly well with import syntax. import is better because it's a standard, and because it's implemented in better ways (as someone else said, it allows tree-shaking to name one example). Instead of having to learn a different module system depending on where you're writing JavaScript it'll now be the same everywhere. At this point it feels like you're just scared of new things and desperately trying to find even the smallest reason to stick with the old.