Citing from https://threejs.org/docs/#manual/en/introduction/Installation:
Second, Node.js support for ES modules is ... complicated. As of Node.js v12, the core library can be imported as a CommonJS module, with require('three'). However, most example components in examples/jsm cannot. Future versions of Node.js may resolve this, but in the meantime you may need to use workarounds like esm to enable your Node.js application to recognize ES modules.
As of latest Node (14.5.0 at the time of writing), ESM support in Node works as expected and nothing special is required (like using CLI flags or MJS extension). Therefore I would like to discuss whether it's appropriate to remove this section of the document.
If it is deemed so, I'll be happy to send in a PR removing it or rewording it.
Perhaps a good time would be once 14 becomes LTS (in October I think) ?
Yeah, that makes sense. I'll ping this issue when it does.
It might also be necessary to use older versions of node until headless-gl has a prebuilt binary for v14. Building it from source has been iffy for me, and a WebGL alternative is important for three.js.
People are likely to be using Node v12 for at least another few years, judging from previous Node releases.
We could change the paragraph:
Support for modules in older versions of Node is... complicated. If you're using Node v14 or higher, you should have no problems. However, for Node.js v12 and below, while the core library can be imported as a CommonJS module using
require('three')
, most example components in examples/jsm cannot. For these, you may need to use workarounds like esm to enable ES modules in your Node.js application.
As of latest Node (14.5.0 at the time of writing), ESM support in Node works as expected and nothing special is required (like using CLI flags or MJS extension).
Sorry for asking but you say nothing special is required. I have just upgrade to node v14.6.0
and if I now try to execute one of the three.js
converter scripts (e.g. obj2three) via:
node utils/converters/obj2three.js examples/models/obj/emerald.obj
I still get:
Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
It works if I modify the package.json. But it appears it does not "just work".
@Mugen87 I think (but I might be wrong) that that's expected - that it is a Node thing. Similarly to how in browser JavaScript when you want to use ESM, you need to use <script type="module">
. I am not aware of any plans on the Node's side to make the module
package type the default (and I don't think it would happen without it also happening on the browser side which I find even more unlikely as it would be a big breaking change).
Okay, thanks for the clarification. I think the documentation should reflect this since many users (especially beginners) will do the same (naive) thing I did^^. So it should at least mention that it's required to configure package.json
in order to run ES6 module based scripts like above.
It's true, I will patch this PR to include this when I have a chance.
I think this is an issue, not a PR 馃槆 .
Oh yeah, sorry, I have an associated PR for this somewhere.
I guess this can be closed then?
TBH, I don't feel strong about this. I suggested https://github.com/mrdoob/three.js/issues/19830#issuecomment-664913548 but one could also argue that it's not the task of the three.js
documentation to explain how to use ES6 modules with node.
Closing for now.
Most helpful comment
Okay, thanks for the clarification. I think the documentation should reflect this since many users (especially beginners) will do the same (naive) thing I did^^. So it should at least mention that it's required to configure
package.json
in order to run ES6 module based scripts like above.