Install Three Js
Install Three Js
Project structure
Every three.js project needs at least one HTML file to define the webpage, and a
JavaScript file to run your three.js code. The structure and naming choices below
aren't required, but will be used throughout this guide for consistency.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
body { margin: 0; }
</style>
</head>
<body>
<script type="module" src="/main.js"></script>
</body>
</html>
main.js
import * as THREE from 'three';
...
public/
The public/ folder is sometimes also called a "static" folder, because the files it
contains are pushed to the website unchanged. Usually textures, audio, and 3D
models will go here.
Now that we've set up the basic project structure, we need a way to run the project
locally and access it through a web browser. Installation and local development can
be accomplished with npm and a build tool, or by importing three.js from a CDN.
Both options are explained in the sections below.
Install Node.js. We'll need it to load manage dependencies and to run our build
tool.
Install three.js and a build tool, Vite, using a terminal in your project folder.
Vite will be used during development, but it isn't part of the final webpage. If
you prefer to use another build tool, that's fine — we support modern build tools
that can import ES Modules.
# three.js
npm install --save three
# vite
npm install --save-dev vite
Installation added node_modules/ and package.json to my project. What are they?
Improve your editor auto-completion with jsconfig or tsconfig
From your terminal, run:
npx vite
What is npx?
If everything went well, you'll see a URL like http://localhost:5173 appear in your
terminal, and can open that URL to see your web application.
The page will be blank — you're ready to create a scene.
If you want to learn more about these tools before you continue, see:
We imported code from 'three' (an npm package) in main.js, and web browsers don't
know what that means. In index.html we'll need to add an import map defining where
to get the package. Put the code below inside the <head></head> tag, after the
styles.
<script type="importmap">
{
"imports": {
"three":
"https://cdn.jsdelivr.net/npm/three@<version>/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@<version>/examples/jsm/"
}
}
</script>
Don't forget to replace <version> with an actual version of three.js, like
"v0.149.0". The most recent version can be found on the npm version list.
We'll also need to run a local server to host these files at URL where the web
browser can access them. While it's technically possible to double-click an HTML
file and open it in your browser, important features that we'll later implement, do
not work when the page is opened this way, for security reasons.
Install Node.js, then run serve to start a local server in the project's directory:
npx serve .
If everything went well, you'll see a URL like http://localhost:3000 appear in your
terminal, and can open that URL to see your web application.
The page will be blank — you're ready to create a scene.
Many other local static servers are available — some use different languages
instead of Node.js, and others are desktop applications. They all work basically
the same way, and we've provided a few alternatives below.
IMPORTANT: Import all dependencies from the same version of three.js, and from the
same CDN. Mixing files from different sources may cause duplicate code to be
included, or even break the application in unexpected ways.
Addons
Out of the box, three.js includes the fundamentals of a 3D engine. Other three.js
components — such as controls, loaders, and post-processing effects — are part of
the addons/ directory. Addons do not need to be installed separately, but do need
to be imported separately.
The example below shows how to import three.js with the OrbitControls and
GLTFLoader addons. Where necessary, this will also be mentioned in each addon's
documentation or examples.
Next Steps
You're now ready to create a sc