ES6 example modules do not provide exports (in node)
See original GitHub issueDescription of the problem
Following https://threejs.org/docs/#manual/en/introduction/Import-via-modules
import * as THREE from 'three';
import { STLLoader } from 'three/examples/jsm/loaders/STLLoader.js';
in an index.mjs
for Node results in
import { STLLoader } from 'three/examples/jsm/loaders/STLLoader.js';
^^^^^^^^^^^^^
SyntaxError: The requested module 'three/examples/jsm/loaders/STLLoader.js' does not provide an export named 'STLLoader'
I 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
- r117
Browser
- node 12.18.0
OS
- macOS
Hardware Requirements (graphics card, VR Device, …)
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (1 by maintainers)
Top Results From Across the Web
Jest, ES6 modules "does not provide export named"
The problem I have is that my app is built with ES6 modules and Jest ES6 modules is experimental and it seems that...
Read more >Node Module Exports Explained - freeCodeCamp
This is a common module exports mistake that people who are starting out with Node.js often make. They assign exports to a new...
Read more >ES6 Modules and How to Use Import and Export in JavaScript
You can export members one by one. What's not exported won't be available directly outside the module: export const myNumbers = [ ...
Read more >16. Modules - Exploring JS
Modules that only export single values are very popular in the Node.js community. But they are also common in frontend development where you...
Read more >ECMAScript modules | Node.js v19.3.0 Documentation
When importing CommonJS modules, the module.exports object is provided as the default export. Named exports may be available, provided by static analysis as...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In the package.json you can actually specify that a package is of type
module
and node will automatically try to load everything as module, meaning you don’t have to specify the extension asmjs
. They suggest denoting the “type” of the package whether it’s commonjs or module in order to future proof the package so it should probably be done anyway. You can see in the docs here.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
"type" : "module"
should enable everything to work when the user is explicitly importing thebuild/three.module.js
file in their application.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
"type": "json"
in the rootpackage.json
because the source code is not entirely modules, and renaming things to.cjs
or.mjs
is not going to happen. Perhaps it would be possible to put{"type": "module"}
intoexamples/jsm/package.json
and just affect those files; if someone would like to test that with Node.js please do.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.