ThreeJS Installation

You can use npm and modern build tools to install three.js, or you can get started quickly with static hosting or a CDN. Installing from npm is the best option for the majority of users.

Whichever option you select, make sure that all files are imported from the same version of the library. Combining files from different sources may result in duplicate code or even break the application in unexpected ways.

All three.js installation methods rely on ES modules (see Eloquent JavaScript: ECMAScript Modules), which allow you to include only the parts of the library that are required in the final project.

Install from npm

To install the three npm modules, launch a terminal window in your project's directory and type:

npm install --save three

The installation package will be downloaded and installed. Then you can incorporate it into your code:

// Option 1: Import the entire three.js core library.
import * as THREE from 'three';


const scene = new THREE.Scene();
// Option 2: Import just the parts you need.
import { Scene } from 'three';


const scene = new Scene();

When you install from npm, you'll almost always use a bundling tool to combine all of the packages your project requires into a single JavaScript file. While any modern JavaScript bundler can be used with three.js, webpack is the most popular option.

Not all features are directly accessible via the three modules (also called a "bare import"). Controls, loaders, and post-processing effects, among other popular library components, must be imported from the examples/jsm subfolder.

Install from CDN or Static Hosting

The three.js library can be used without a build system by uploading files to your own web server or by using a CDN that already exists.

Because the library is based on ES modules, any script that refers to it must include the type="module" attribute, as shown below:

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

Examples

Three.js's core focuses on the most important components of a 3D engine. The examples/jsm directory contains a plethora of other useful components, such as controls, loaders, and post-processing effects. They're called "examples" because, while they can be used as-is, they're also meant to be remixed and customized. These components are always in sync with the core library, whereas comparable third-party packages on npm are maintained by different people and may be out of date.

Exemplifications do not need to be installed separately, but they must be imported separately. If three.js was installed with npm, you can use the following command to load the OrbitControls component:

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';


const controls = new OrbitControls();
If three.js was installed from a CDN, use the same CDN to install other components:


<script type="module">


  // Find the latest version by visiting https://cdn.skypack.dev/three.


  import { OrbitControls } from 'https://cdn.skypack.dev/three@<version>/examples/jsm/controls/OrbitControls.js';


  const controls = new OrbitControls();


</script>

It is critical that all files use the same version number. Do not import examples from different versions or use examples from versions other than the three.js library itself.

Compatibility

CommonJS Imports

Although most modern JavaScript bundlers support ES modules by default, some older build tools may not. In those cases, you can probably configure the bundler to understand ES modules: Browserify, for example, only requires the babelify plugin.

Import Maps

When installing from npm, the imported paths differ from when installing from static hosting or a CDN. We are aware that this is an ergonomic issue for both user groups. Developers using build tools and bundlers prefer bare package specifiers (e.g. 'three') over relative paths, and files in the examples/ folder make use of relative references to three.module.js that do not conform to this expectation. Those who do not use build tools — whether for quick prototyping, learning, or personal preference — may dislike relative imports, which require specific folder structures and are less forgiving than a global THREE.* namespace.

When import maps become widely available, we hope to replace them with bare package specifiers to the npm package name, 'three.' This more closely matches build tool expectations for npm packages, allowing both groups of users to write exactly the same code when importing a file. Users who prefer not to use build tools can direct all imports to a CDN or static file folder using a simple JSON mapping.

Node.js

Three.js in Node.js can be difficult to use for two reasons:

For starters, because three.js is designed for the web, it relies on browser and DOM APIs that aren't always available in Node.js. Some of these issues can be addressed by using shims such as headless-gl or by replacing components such as TextureLoader with custom alternatives. Other DOM APIs may be deeply entwined with the code that uses them, making them more difficult to work around. We welcome simple and maintainable pull requests to improve Node.js support, but we recommend that you first open an issue to discuss your improvements.

Second, support for ES modules in Node.js is complicated. With require('three'), the core library can now be imported as a CommonJS module in Node.js v12.

Most example components in examples/jsm, on the other hand, cannot. Future Node.js versions may address this, but in the meantime, you may need to use workarounds such as esm to enable your Node.js application to recognize ES modules.


Related Topics

ThreeJS Animation Systems

You may animate many aspects of your models with the three.js animation system, including the bones of a skinned and rigged model, morph targets, multiple material properties (colours, opacity, booleans),...

3 minutes read.

ThreeJS Object3D

This is the base class for most three.js objects, and it provides a set of properties and methods for manipulating objects in 3D space. It should be noted that this can...

9 minutes read.

ThreeJS Keyframe Tracks and Animation

A KeyframeTrack is a timed series of keyframes that are made up of lists of times and related values and are used to animate an object's unique property. BooleanKeyframeTrack A Track of...

3 minutes read.

ThreeJS Material - Mesh

MeshBasicMaterial A material for drawing geometries in a simple shaded (flat or wireframe) way. This material is not affected by lights. Constructor MeshBasicMaterial( parameters : Object ) parameters - (optional) an object with one or...

24 minutes read.

ThreeJS Geometries-1

BoxGeometry BoxGeometry is a geometry class for a rectangular cuboid with a given 'width', 'height', and 'depth'. On creation, the cuboid is centered on the origin, with each edge parallel to...

7 minutes read.

ThreeJS AnimationMixer

The AnimationMixer is a player that allows you to play animations on a specific object in the scene. When many items in a scene are animated individually, each object may...

2 minutes read.

ThreeJS Objects

Bone A bone that is part of a Skeleton. The skeleton in turn is used by the SkinnedMesh. Bones are almost identical to a blank Object3D. Code Example const root = new THREE.Bone(); const...

12 minutes read.

ThreeJS Cameras

ArrayCamera ArrayCamera can be used to render a scene using a preset group of cameras quickly. This is an important feature of VR scene rendering performance. An array of sub cameras is...

6 minutes read.

ThreeJS BufferGeometry

A mesh, line, or point geometry representation. Included within buffers are vertex positions, face indices, normals, colours, UVs, and custom attributes, lowering the cost of passing all of this data...

5 minutes read.

ThreeJS Loading 3D Models

There are hundreds of file formats for 3D models, each with its own purpose, set of features, and level of complexity. Although three.js has a lot of loaders, choosing the...

2 minutes read.

ThreeJS BufferAttribute

This class stores data for an attribute associated with a BufferGeometry (such as vertex positions, face indices, normals, colours, UVs, and any custom attributes), allowing for more efficient data passing...

4 minutes read.

ThreeJS Creating VR Content

This article gives a quick introduction of the main components of a three.js-based web-based VR application. Workflow      First, you have to include VRButton.js into your project. import { VRButton } from...

1 minute read.

ThreeJS Raycaster

This class is intended to help with raycasting. Raycasting is used for a variety of purposes, including mouse picking (determining which objects in 3D space the mouse is over). Code Example const...

4 minutes read.

ThreeJS Renderers

WebGLMultipleRenderTargets A special render target that enables a fragment shader to write to several textures. This approach is useful for advanced rendering techniques like post-processing or deferred rendering. Heads up: WebGLMultipleRenderTargets...

15 minutes read.

ThreeJS Compatibiliy Check and Running Locally

Even though this is becoming less of an issue, some devices or browsers may still be incompatible with WebGL. The method below enables you to check if it is supported...

3 minutes read.

ThreeJS Audio

It is used to create a non-positional (global ) audio object. Code Example: // create an AudioListener and add it to the camera const listener = new THREE.AudioListener(); camera.add( listener ); // create a global...

6 minutes read.

ThreeJS Keyframe Track

A KeyframeTrack is a timed series of keyframes that are made up of lists of times and related values and are used to animate an object's unique property. The "Animation System"...

4 minutes read.

ThreeJS Materials 3

RawShaderMaterial This class works just like ShaderMaterial, except that definitions of built-in uniforms and attributes are not automatically prepended to the GLSL shader code. Code Example const material = new THREE.RawShaderMaterial( {    ...

9 minutes read.

ThreeJS Scenes

Fog This class contains the parameters that define linear fog, i.e., that grows linearly denser with the distance. Constructor Fog( color : Integer, near : Float, far : Float ) The color parameter is...

3 minutes read.

ThreeJS Material

Abstract base class for materials. Materials describe the appearance of objects. They are defined in a (mostly) renderer-independent way, so you don't have to rewrite materials if you decide to use...

6 minutes read.