Back
Setup guide

Before you can build a three.js game in the browser, your computer needs a code editor, Node.js and npm, and the three.js library itself. This guide sets up all of them and ends with a spinning cube to prove it works.

  • 6 short steps
  • A check after every step
  • Windows and Mac

This guide supports Lesson 4 of Rigging and Animating a Creature in WebGL. From that lesson on, the crab runs in a web page built with three.js, so everything below needs to be in place first.

If you have never opened a terminal or installed a developer tool before, that is fine. Every step ends with a quick check, so you always know whether it worked before you move on.

This guide uses a separate practice folder, so nothing here touches your course project. Lesson 4 repeats the project commands for the real build, and having done them once here makes that lesson much easier to follow.

What You'll Do in This Post

  • Step 1 Install Node.js (npm comes with it)
  • Step 2 Install VS Code
  • Step 3 Create a practice folder and open a terminal
  • Step 4 Start the project with npm
  • Step 5 Install three.js and Vite, and add two scripts
  • Step 6 Run a test page with a spinning cube

Three terms to know first. Node.js lets your computer run JavaScript outside a web browser, which the tools in this guide need. npm (Node Package Manager) downloads libraries such as three.js for you. Vite is a small tool that serves your page on your own computer while you work on it.

Diagram: VS Code, Node.js and npm, the project folder, and the browser, and how they connect
The big picture: you type in VS Code, npm fetches the libraries, and Vite shows the result in your browser.

1 Install Node.js

Go to nodejs.org, download the version marked LTS (long-term support), and run the installer. Accept the default options. The installer sets up npm at the same time.

Vite needs Node.js version 20.19 or newer (or 22.12 or newer). The current LTS download meets this.

Restart VS Code After Installing

If VS Code or a terminal was already open while you installed Node.js, close it and open it again. A terminal that was open before the install cannot see Node.js, and you will see a message saying the command was not found.

Checkpoint

Open a new terminal (Step 3 shows how) and type each of these, pressing Enter after each one:

node --version
npm --version

Each should print a version number. For Node.js it starts with a v, for example one beginning with v22 or v24. If you see two version numbers, Step 1 is done.

2 Install VS Code

VS Code (Visual Studio Code) is a free code editor. It is where you will write the files for your game and run the commands in this guide. If it is already installed from an earlier guide, skip to Step 3.

Download it from code.visualstudio.com and install it like any other program.

Checkpoint

Open VS Code. If you see its welcome screen, Step 2 is done.

3 Create a Practice Folder and Open a Terminal

A terminal is a place to type commands instead of clicking buttons. First, create a folder on your computer for this practice run, for example a folder called three-test on your Desktop. You can delete it when you have finished.

In VS Code, choose File → Open Folder and select that folder. Then open the menu at the top and go to Terminal → New Terminal. A panel opens at the bottom of the window. Every command in the rest of this guide is typed there.

Windows: If npm Says Scripts Are Disabled

On some Windows computers the terminal shows a message such as "npm.ps1 cannot be loaded because running scripts is disabled on this system." You do not need to change any system settings. Click the small arrow next to the plus sign at the top of the terminal panel, choose Command Prompt, and type the commands there instead.

Checkpoint

Your folder name should appear at the top of the file explorer on the left, and the terminal panel should show a path that ends in the same folder name. If both match, Step 3 is done.

4 Start the Project with npm

In the terminal, type:

npm init -y

This creates a file called package.json in your folder. It is the project's list of the libraries it uses, and npm keeps it up to date for you.

Checkpoint

A file called package.json appears in the file explorer on the left. If it is there, Step 4 is done.

5 Install three.js and Vite

Type these two commands, one after the other, waiting for each to finish:

npm install --save three
npm install --save-dev vite

The first adds the three.js library to your project. The second adds Vite, which is only used while you are building the game and is not part of the finished page. A new folder called node_modules appears. It holds the downloaded libraries, so leave it alone.

Now open package.json and find the part called "scripts". Replace it with these two short commands, so Vite can be started with npm run dev:

"scripts": {
  "dev": "vite",
  "build": "vite build"
}

Keep the commas between sections exactly as they are, and save the file.

Checkpoint

In package.json you should see three listed under dependencies, vite listed under devDependencies, and your two scripts. If you see all three, Step 5 is done.

6 Run a Test Page with a Spinning Cube

This last step proves that everything works together. In VS Code, create two new files in your practice folder.

The first is called index.html. Paste this into it and save:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>three.js test</title>
    <style>body { margin: 0; }</style>
</head>
<body>
    <script type="module" src="/app.js"></script>
</body>
</html>

The second is called app.js. Paste this into it and save:

import * as THREE from 'three';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 3;

const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const cube = new THREE.Mesh(
    new THREE.BoxGeometry(1, 1, 1),
    new THREE.MeshNormalMaterial()
);
scene.add(cube);

function animate() {
    cube.rotation.x += 0.01;
    cube.rotation.y += 0.01;
    renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
The practice folder after Step 6, with node_modules, app.js, index.html, package-lock.json and package.json
Your practice folder should look like this once both files are saved.

Now start Vite from the terminal:

npm run dev

After a moment the terminal shows an address, usually http://localhost:5173. Hold Ctrl (Command on a Mac) and click it, or copy it into your browser.

A browser window at localhost:5173 showing a cube with pink, green and blue faces on a black background
The test page in your browser.

Checkpoint

You should see a cube with pink, green and blue faces turning slowly on a black background. If you do, Node.js, npm, three.js and Vite are all working. To stop the server, click in the terminal and press Ctrl and C together.

If the Page Stays Blank

Check three things. First, both files must be in the same folder as package.json. Second, the file names must be exactly index.html and app.js. Third, open the browser's developer tools (press F12) and look at the Console tab, where a red message usually names the problem.

Your Computer Is Ready

VS Code, Node.js, npm, three.js and Vite are installed, and you have seen a three.js scene run in your own browser. This is the same setup the crab game uses, and the same commands Lesson 4 walks through.

Next: return to Lesson 4 and build the real project. You can delete the practice folder whenever you like.

Stay Connected with CulerLearn

Subscribe for more posts like this on AI, computer vision, and research in progress.

Subscribe