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
ES6 example modules do not provide exports (in node) #19575
Comments
Such imports in |
In the package.json you can actually specify that a package is of type Presumably this would mean that the file in the "main" field should be module rather than commonjs compatible (which we're not doing) but I know has been discussed. At least I believe using |
In my own package it already defines "type": "module". So this doesn't help. Try a minimal node package after
I writing as the js files in examples are marked as deprecated now - but until this problem is solved it's not a good idea to remove them ... |
The docs state that |
I don't think we can set Perhaps you can use something like esm in the meantime? |
That is a good point. The workarounds for node.js are harder than the workarounds for the browser here... there are 6+ months before those files are actually removed, so it would be good to figure out a workflow for node.js users. I wonder if putting a |
It would require that the "main" field be set to "three.module.js" in the package.json. Realistically though what would be lost by doing this now? What environment is relying on "main" pointing to a UMD file? I can't think of any. All package bundlers now resolve to "module" by default and using the old script tag method doesn't rely on the package.json file anyway. Node.js seems like the only platform that depends on it and that environment now supports modules -- and in fact we're breaking support in that environment for examples by not putting the module file there. The UMD file can still be built and distributed but I'm just not seeing any value in referencing it in the package.json.
This won't work and just expose the problem we've seen before with codesandbox in node. The app calling I propose we set "main" to point to "build/three.module.js" unless there's some environment or use case that I've missed that will break. But I can't think of one other than older versions of node and it's such a hack to get three.js working with examples in that environment right now anyway. |
I disagree with this — people still use tools like Browserify. Deleting Maybe we'll be in a position to reconsider this after |
I forgot about Browserify. If there are still processes that rely on the main field then I agree it shouldn't be removed, yet. I'm less familiar with Browserify but hypothetically if we were to change main to point to module would users be able to "migrate" by changing all instances of |
Some good news - I hope at least ... node 12.18.0 (which has ESM support) seems to work when the file names are mjs
Then
in
works Please cross check! |
Another update:
works too. Still ... it would be nice to know the recommended node way. |
Yes, that does work but it's unrealistic to change the file extensions because of this use case. Using the
Sounds good. When |
I think this can be closed now? |
Recap — Node.js supports ES6 Modules in modern versions, but makes design choices that cause interoperability to be difficult for library authors who also need to support the web and backward-compatibility. For three.js users, I'd suggest using esm as a workaround in Node.js. For the three.js project, I think our current state is probably the best it can be under the circumstances. We cannot use But with current Node.js support, and our current understanding of the problem, there doesn't appear to be anything to do here. I'd vote to close this as well. |
Description of the problem
Following https://threejs.org/docs/#manual/en/introduction/Import-via-modules
in an
index.mjs
for Node results inI know it's node and not a browser - but some threejs features such as loaders are perfectly usable without display - and the NON-jsm version works perfect in my setup
require('three/examples/js/loaders/STLLoader.js');
BTW: same problem when copy-pasting the example from the website
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
Three.js version
Browser
OS
Hardware Requirements (graphics card, VR Device, ...)
The text was updated successfully, but these errors were encountered: