question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ES6 example modules do not provide exports (in node)

See original GitHub issue
Description 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:closed
  • Created 3 years ago
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
gkjohnsoncommented, Jun 6, 2020

Such imports in node.js only work if the imported files are also mjs files, which is obviously not true for the npm package.

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 as mjs. 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 the build/three.module.js file in their application.

3reactions
donmccurdycommented, May 11, 2021

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 root package.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"} into examples/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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found