4 ms·
Those have been supported for several years now :) You need to tell Node to use the format by either naming your file `.mjs` or setting `"type": "module"` in y
by inglor 5y ago
Those have been supported for several years now :)
You need to tell Node to use the format by either naming your file `.mjs` or setting `"type": "module"` in your `package.json` file.
> Also why do so many libraries have this strange 5 line header code for this umd, amd, business. Is that to make their packages work with nodejs?
That's just old for "adapt the module system" from the old days and libraries just didn't bother updating.
I still use amd in my job at Microsoft though for some things so I guess it's not useless :)
- mschuetz 5y ago> either naming your file `.mjs` But why? This makes it much harder to ensure that identical code works in browser and nodejs, to the point where I sometimes just can't use nodejs. And I don't use package.json, so having to name files ".mjs" is a really painfull restriction.
- inglor 5y agoBrowsers will happily accept `.mjs` files they don't actually care about file extensions only about `Content-Type` and type=module on the script tag :]
- mschuetz 5y agoPlenty of (third-party) imports specifically reference "*.js" files so no, this doesn't work.
- distrill 5y agowhy are you writing node applications without using package.json? it sounds like you're looking to have a problem with javascript tbh. don't use it, whatever
- mschuetz 5y agoThere is absolutely no need for a package.json when using ES6 imports without a build step. Package.json doesn't do anything when I'm loading the code in a browser, why would I have to use it when I want to run the same JS code with node?
- distrill 5y agothis is a silly point. it's a different runtime, there are also different apis available - as is the point of this article. node is not an internet browser. but ok
- Natfan 5y agoIf you're hosting the browser-based version over a server like Apache2 (to host the files), you could add a `RewriteRule` to redirect queries to `.js` resources to their `.mjs` counter-part files. Example: RewriteRule "(.*).js$" "$1.mjs" [R=301 NC] Reference documentation: https://httpd.apache.org/docs/2.4/rewrite/intro.html https://httpd.apache.org/docs/2.4/rewrite/intro.html
- tomphoolery 5y ago"supported" is somewhat of a misnomer IMHO. It's only supported if the other tools you're using support ESM. Otherwise, it's not. And it's really annoying when libraries choose to go full ESM because it breaks things like tests and deployments in weird ways. It's a pain in the ass.
- icholy 5y agoNode supports ESM, full stop.
- mschuetz 5y agoNo, it doesn't. It forces you to use a special extension that breaks the code in browsers.
- rattlesnakedave 5y agoIt's really annoying when libraries don't support a 7 year old standard, as well.
- IshKebab 5y agoSeveral years is a bit of an exaggeration. They were "experimental" until 20 months ago.