Getting Started: Import Maps, npm & Vite
There are two ways to get Three.js into a page: load ES modules straight from a CDN with an import map (no build step, good for experiments and for this course's playgrounds), or install it from npm and bundle with a tool such as Vite (what you want for a real game). Both use the same import statements, so code moves between them unchanged.
Option 1: an import map
Browsers resolve bare specifiers such as 'three' through an import map: a JSON block that says which URL each name stands for. Always pin an exact version.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My first Three.js page</title>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.186.1/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.186.1/examples/jsm/"
}
}
</script>
</head>
<body>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
console.log(THREE.REVISION); // "186"
</script>
</body>
</html>The second entry ends in /, so it maps a whole folder: three/addons/controls/OrbitControls.js becomes …/examples/jsm/controls/OrbitControls.js. Addons are everything outside the core: controls, loaders, post-processing, utilities. They live in the package's examples/jsm folder, and three/addons/… is the official alias for it.
Note: Module scripts are fetched with CORS, so open such a page through a local web server (
npx serve, the VS Code Live Server extension,python -m http.server), not by double-clicking the file.file://pages can't load modules.
The import map in this course
Every html run playground here already has an import map with these entries, so lessons just write import:
| Specifier | Library | Version |
|---|---|---|
three |
Three.js core | 0.186.1 (r186) |
three/addons/… |
Three.js addons (examples/jsm) |
0.186.1 |
cannon-es |
cannon-es physics | 0.20.0 |
@dimforge/rapier3d-compat |
Rapier 3D physics (WASM, inlined) | 0.21.0 |
This playground prints what it loaded. Check the console panel under the frame:
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import * as CANNON from 'cannon-es';
import RAPIER from '@dimforge/rapier3d-compat';
console.log('three revision:', THREE.REVISION);
console.log('OrbitControls is a', typeof OrbitControls);
console.log('cannon-es World is a', typeof CANNON.World);
await RAPIER.init(); // Rapier must load its WASM before use
console.log('Rapier version:', RAPIER.version());
const canvas = document.createElement('canvas');
console.log('WebGL2 available:', !!canvas.getContext('webgl2'));
</script>Option 2: npm and Vite
For a real project, install packages with npm and let a bundler resolve, tree-shake and minify them. Vite is the usual choice for Three.js: a fast dev server with hot reload, and a production build in one command.
npm create vite@latest my-game -- --template vanilla-ts
cd my-game
npm install three
npm install -D @types/three
npm run devThree.js is written in JavaScript; its TypeScript types come from the separate @types/three package. Keep the two on the same release number (both 0.186.x). Then in src/main.ts:
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);The package's exports field maps three to build/three.module.js and three/addons/* to examples/jsm/*, which is why the same imports work under Vite and with the import map. The Vite, TypeScript & Shipping lesson covers the build, asset handling and deployment.
| Import map + CDN | npm + Vite | |
|---|---|---|
| Setup | one HTML file | Node.js, npm install |
| Reload on save | manual | hot module replacement |
| TypeScript, autocomplete | no | yes |
| Tree-shaking, minifying | no (you download the full module) | yes |
| Offline / reproducible builds | depends on the CDN | lockfile + your own host |
| Good for | experiments, demos, jams with no tooling | anything you ship |
Pin the version
Three.js has no semantic versioning in the usual sense: every release can change APIs, and the changelog for each one lists breaking changes under the release notes on GitHub, plus a migration guide on the project's wiki. So:
- Pin an exact version (
"three": "0.186.1"or a lockfile), neverlatestfrom a CDN. - Upgrade on purpose, a few releases at a time, and read the migration notes for each one.
- Watch the console after an upgrade. Three.js warns about deprecated APIs for a while before removing them, for example
THREE.Clock: This module has been deprecated. Please use THREE.Timer instead.
Looking ahead: WebGPU
Besides WebGLRenderer, Three.js ships a WebGPURenderer with its own entry point. It uses WebGPU where the browser supports it and falls back to a WebGL 2 backend otherwise, and its materials are built from TSL (Three Shading Language), a JavaScript node system that compiles to both WGSL and GLSL. It is where new rendering features land, but WebGLRenderer remains the mature, widely tested choice, and it is what this course uses (the playgrounds' import map does not include three/webgpu).
// Not runnable here: a WebGPU setup in an npm project.
import * as THREE from 'three/webgpu';
const renderer = new THREE.WebGPURenderer({ antialias: true });
await renderer.init(); // picks WebGPU or the WebGL 2 fallback
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
// scene, camera and meshes are created exactly as with WebGLRenderer
renderer.setAnimationLoop(() => renderer.render(scene, camera));Most scene code (meshes, standard materials, lights, cameras, loaders) is the same under both renderers. What differs is custom shader code (ShaderMaterial with GLSL vs TSL node materials) and some post-processing. The Babylon.js, PlayCanvas & WebGPU lesson returns to this.
Test yourself
An import map contains "three/addons/": "https://…/three@0.186.1/examples/jsm/". Which URL does three/addons/loaders/GLTFLoader.js load?
- https://…/three@0.186.1/addons/loaders/GLTFLoader.js
- https://…/three@0.186.1/examples/jsm/loaders/GLTFLoader.js
- https://…/three@latest/examples/jsm/loaders/GLTFLoader.js
- It cannot be resolved
Why should you pin an exact Three.js version?
- Newer versions are slower
- Any release can rename or remove APIs, so an unpinned dependency can break your game without a code change
- CDNs refuse unpinned requests
- Pinning enables WebGPU
Where do Three.js TypeScript types come from?
- They are bundled in the
threepackage - The separate
@types/threepackage - Vite generates them
- There are none