I just upgraded to the latest npm version of three.js - 0.132.2. This causes my app to fail in all browsers with this error:
index.html:1 Uncaught TypeError: Failed to resolve module specifier “three”. Relative references must start with either “/”, “./”, or “…/”.
I poked around and noticed that in the three.js repo imports from examples/jsm use ‘build/three.module.js’ while in the npm version they use ‘three’. This throws an error in all browsers preventing my app from running.
Can the npm version be changed to use ‘build/three.module.js’?
Packages published to NPM are meant to be used with a build tool or bundler. Changing the files as you mention above would cause issues with the other parts of the NPM ecosystem.
If you are managing dependencies as static files, you may want to get the files from GitHub instead, or make the necessary changes to the files from NPM.
In general I would recommend using a build tool or bundler like Webpack, Parcel, or Snowpack for a JS application with dependencies.
import * as THREE from "../node_modules/three/build/three.module.js";
import { mergeBufferGeometries } from '../node_modules/three/examples/jsm/utils/BufferGeometryUtils.js';
This allows me to run the app and debug without having to repeatedly bundle. I only really need to bundle when I publish for others to use the app.
that would break. package.json has multiple definitions of “three”, hands them out according to the env (browser, esm, cjs, …). bypass that and you destroy namespaces, and essentially you’ll bundle three multiple times in some situations.
like @donmccurdy said, for rollup you need @rollup/plugin-node-resolve, after that everything will be fine. other bundlers have node resolution preconfigured. it’s not in rollup because it’s usually not being used as a client bundler, it’s mostly for packing up distributed libraries.
You can’t use npm or rely on dependencies in that case. Unless you use import maps which is the canonical solution. You can also use script tags and fetch from three/examples/js, or use cdn bundlers, threejs recommends unpkg or skypack.