Though I'm answering this very late but I wanted to clarify a few things here for others.
Answer Criteria
While the official documentation recommends using the module, sometimes it's not possible to use the module-based code or any build tools like webpack.
This answer is for those developers, who want to use the Three.js latest libraries on the web apps which do not use ES6 module based architecture or any build tool.
So if you want to use Three.js on your npm or webpack based web application, follow their recommended official documentations only.
Problem 1 (for non-module based web apps)
Three.js library is already moved to ES6 module based architecture so their official installation documentation shows how to use the module-based architecture-
<script type="module">
// Find the latest version by visiting https://cdn.skypack.dev/three.
import * as THREE from 'https://cdn.skypack.dev/three@<version>';
const scene = new THREE.Scene();
</script>
So even if you browse to https://cdn.skypack.dev/three, you will get URLs of ES6 modules.
Problem 2 (Existing non-module URLs are outdated)
If you Google any three.js examples, almost all the blog posts use outdated URLs. For example-
https://cdnjs.cloudflare.com/ajax/libs/three.js/r123/three.min.js
The problem with these CDN URLs is that, if you replace r123
with the latest one (for example, r138), the URLs won't work. So you won't be able to use the latest releases as the three.js libraries are not updated on these CDNs.
Even if you get the latest three.js library, you will have a hardtime using the examples like OrbitControls
.
Solution
The solution is quite simple. Since the three.js is always up-to-date on https://www.npmjs.com/, you can use CDN providers like https://www.jsdelivr.com/ or https://unpkg.com to pick the assets from npm registry directly.
(skypack.dev & cdnjs.com seems to be far outdated to me)
Now, the URLs are pretty simple-
<script src="https://cdn.jsdelivr.net/npm/three/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three/examples/js/loaders/GLTFLoader.min.js"></script>
The above URL will always point to the latest three.js release which might not be backward compatible with your code. So better to use the pinned versions-
<script src="https://cdn.jsdelivr.net/npm/three@0.138.3/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.138.3/examples/js/loaders/GLTFLoader.min.js"></script>
For other popular parts of the library — such as controls, loaders, and post-processing effects, make sure you are using examples/js
not the examples/jsm
as jsm
stands for JS modules so it won't work with non-module codebase (took me 2-3 hours to spot this silly mistake).
Remember, you can always browse the directory by appending a /
at the end-
