How the classes in the early Three.js build connect, and the real errors hit while building it, with exactly how each was diagnosed and fixed. It covers the build up to the App class. Come back to it whenever something breaks.
Setting Up Node.js
The Three.js lessons assume Node.js is already installed on your machine. It is not shown on camera, because walking through an installer adds nothing to the teaching, so it is covered here instead. For a fuller walkthrough that also covers VS Code and Three.js, see the setup guide.
What Is Node.js
Node.js lets JavaScript run outside a browser, directly on your own computer. That is what makes installing packages and running a local development server possible. Without it, none of the npm commands used in this course would work.
npm is not a separate install. npm, Node's package manager, comes bundled with Node.js. Installing Node is the only thing you need to do.
Installing It
- Go to nodejs.org. The homepage offers a download button, usually labelled with the current LTS (Long-Term Support) version. Choose that one. LTS is the stable, recommended release, not the newest experimental one.
- Run the installer for your operating system, Windows or macOS. Accept the default options throughout.
- Restart your terminal (or VS Code, if its integrated terminal was already open) once installation finishes, so that it picks up the new commands.
Confirming It Worked
Open a terminal and run both of these:
node -v
npm -v
Each should print a version number. If either command is not recognised, the most common cause is a terminal window that was already open before installation finished. Close it and open a fresh one.
Why this matters for debugging: if any npm install or npm run command in the Three.js lessons fails immediately with an error about the command not being found, check this first. It is not a problem with your project.
How the Classes Connect
The Three.js build is not one script. It is a set of small classes, each with one job, that pass specific pieces of information to each other. Knowing who talks to whom makes the debugging in the rest of this page make sense. Most of the bugs came from two classes reaching for the same thing at the same time, not from any one class being wrong on its own.
Read this list from top to bottom. Each layer depends on the one above it.
- Orchestrator: App. Creates and owns every class below, and runs the single render loop.
- Foundation: SceneManager. Owns the scene, camera, renderer and OrbitControls.
- Content: ModelLoader, Environment, AnimationController. Load the model file, make the ground plane, and play clips on the model.
- Behaviour: CharacterController, DistanceTracker. One moves the model and tells AnimationController when to switch clips. The other watches the model's position independently.
- Interface: UIManager. Builds the buttons, dropdown and HUD, and reads from the three behaviour and animation classes above it.
SceneManager Is the Foundation, Not a Peer
Every other class either needs something SceneManager owns, its scene property to add things to, or does not need it at all. It is created first, and nothing can exist before it, because there is no scene to put a model or a ground plane into until the renderer and camera are running.
Why this matters for debugging: if nothing renders at all, the fault is almost always here, not deeper in the model loading or animation code. Confirm that the scene, camera and renderer are correct before troubleshooting anything downstream of them.
Class by Class
A quick reference for what each class does and what it connects to.
- SceneManager: owns the scene, camera, renderer and OrbitControls. Everything else adds content to its scene.
- ModelLoader: loads the GLB file and hands back the parsed result. Used once, by App, to get the model and its animations.
- Environment: creates the ground plane the creature stands and moves on. Adds directly to SceneManager's scene, and nothing reads from it.
- AnimationController: plays and fades between named animation clips. Called by both CharacterController and UIManager.
- CharacterController: moves the model and decides which animation should be playing. Calls into AnimationController, and is driven by UIManager's buttons.
- DistanceTracker: measures how far the model has actually moved, frame to frame. Reads the model directly, and is read by UIManager for the HUD.
- UIManager: builds the buttons, dropdown and HUD. Reads from CharacterController, AnimationController and DistanceTracker.
- App: creates every class above and runs the single render loop. It is the entry point, and nothing depends on it.
The Startup Sequence
The connections show who talks to whom, but not the order things happen in when the app runs. That order is where the timing bugs on this page come from.
- App's constructor runs first. It creates SceneManager, Environment, ModelLoader and Timer, in that order. Nothing else exists yet.
App.init()is called, starting two things at once: the model load, and the render loop.- The model load is asynchronous, so it takes a moment. The render loop is already running by this point, and it checks for classes that do not exist yet, skipping them rather than raising an error.
- Once the model finishes loading, AnimationController, CharacterController, DistanceTracker and UIManager are all created together, in that order, since each depends on the one before it.
- From then on, every frame calls each class's own
update()method in sequence, then renders the result.
Why this matters for debugging: the gap between steps 2 and 4 is exactly what caused the "characterController is not defined" bug below. Any class created inside the model-loading callback has to cope with something else trying to use it before that callback has finished.
Design Choices Worth Noticing
ModelLoader Does Not Know the Scene Exists
This is deliberate. ModelLoader's only job is fetching a file and handing back the parsed result. It never calls scene.add() itself. That is left to whatever code calls .load(). The benefit is that ModelLoader can be reused for any model, in any scene, without modification.
AnimationController Does Not Know Why It Was Called
AnimationController exposes exactly two actions, play(name) and stop(), and it runs them without asking why. It does not know whether a walk clip started because the character is genuinely moving, or because someone picked it from a dropdown. That is intentional, but it is also where two of the bugs below came from, because two different classes can both legitimately call into it.
CharacterController Is the Actual Connector
CharacterController is the one class that holds a reference to another custom class, animationController, rather than just raw Three.js objects. Its job spans two concerns: moving the model's position and rotation each frame, and deciding, based on that movement, which animation should be playing. It translates "what the user is doing" into "what the character looks like it is doing."
Why this matters for debugging: any bug where movement and animation disagree, the character moving without walking or walking without moving, lives in this class's update() and _syncAnimation() methods, not in AnimationController itself.
DistanceTracker Deliberately Knows Nothing About CharacterController
This is the opposite choice to CharacterController. DistanceTracker only ever receives the model itself, never characterController. It watches model.position change from frame to frame and measures it, with no idea whether that change came from keyboard input, a UI button or anything else. The distance counter would keep working even if the movement system were rebuilt entirely, because it measures an outcome, not a cause.
UIManager Sits on Top of Everything
UIManager is the only class that receives three other custom classes at once: characterController, animationController and distanceTracker. It does no game logic. Its whole job is translating clicks and selections into calls on those three, and reading distanceTracker.getDistance() back out to display it. This is also why the dropdown bug below happened. UIManager's dropdown calls animationController.play() directly, while CharacterController is, separately and continuously, also calling into the same AnimationController. Two classes had a legitimate path to the same method, and neither knew about the other's calls.
App Is What Builds All the Others
App sits above the layers, not inside them, because it creates every other class rather than depending on what they do. Before App existed, the render loop, the asynchronous model-loading callback and the keyboard test controls were loose code at the top level of app.js. It worked, but it broke the rule that no logic should exist outside a class except a single entry-point script. App's constructor creates SceneManager first, then its init() method loads the model and creates everything downstream once it is ready. It is the same sequence the loose code followed, now owned by one object instead of scattered across the file.
Why this matters for debugging: every class that depends on the asynchronously loaded model, CharacterController, DistanceTracker and UIManager, is declared as this.x = null in App's constructor and only assigned inside the .then() callback in init(). That is the same guarded pattern as the scoping fix below, now built into the architecture instead of something to remember by hand on every new class.
Troubleshooting: Scene Setup and Timing
"THREE.Timer is not a constructor", or the import fails silently
THREE.Clock was deprecated as of Three.js r183 and replaced by Timer. Unlike Clock, Timer is an addon, not part of the core library, so it needs its own import path:
import { Timer } from 'three/addons/misc/Timer.js';
Timer also works slightly differently. It needs an explicit .update() call each frame before you read values from it:
timer.update();
const delta = timer.getDelta();
This sits entirely inside App's render loop. It has no relationship to any of the other custom classes. Every class that needs a delta value (AnimationController, CharacterController) receives it as a plain number, not a reference to the timer itself.
"characterController is not defined" (or any controller class) in the render loop
If a controller is declared with const inside the model loader's .then() callback, it only exists inside that callback. Referencing it anywhere else, such as the animation loop or keyboard listeners, throws a ReferenceError. Declare it with let at the top level, then assign it, without const, inside the callback:
let characterController;
modelLoader.load(path).then((gltf) => {
characterController = new CharacterController(model, animationController);
});
This bug is really a timing problem, not a logic problem. ModelLoader.load() is asynchronous, and the render loop starts immediately, before the model has necessarily finished loading. Every class created inside that .then() callback needs the same guarded pattern: declared outside, assigned inside, and checked for existence before use inside the loop.
Troubleshooting: Animation and Movement
Console flooded with "No animation clip named 'idle' was found"
This fires every single frame if the code tries to play a clip that does not exist in the exported model. Rather than calling play('idle') on a missing clip, add a stop() method that fades out cleanly, and use that until a real idle animation exists:
stop() {
if (this.currentAction) {
this.currentAction.fadeOut(0.3);
}
this.currentAction = null;
}
The character walks in place and its position never advances
If the rig's root bone has forward translation baked into the walk animation, which is common when the clip is built to loop, that animation-driven position can conflict with code that is also trying to move the model. The animation resets the position every loop, overwriting the movement code's updates. Check the browser console for the root bone's exact track name by logging each clip's tracks. Then either strip that bone's .position track from the clip before it plays, or re-export the model with a separate, dedicated idle animation. The re-export resolved this in the course build without any manual track filtering.
The same underlying issue as the dropdown bug below: two systems writing to the same property. Here it is the animation clip and CharacterController.update() both trying to set the model's position each frame.
Manually selecting an animation from the dropdown does not stick, and it reverts almost immediately
As covered above, UIManager's dropdown and CharacterController's automatic sync both call into AnimationController independently. If the sync method runs every frame and unconditionally re-asserts the current state, it overrides a manual selection within a fraction of a second. Fix it by acting only when the state actually changes, not on every frame:
_syncAnimation() {
if (this.isMoving === this._wasMoving) return;
this._wasMoving = this.isMoving;
if (this.isMoving) {
this.animationController.play('walk_animation');
} else {
this.animationController.stop();
}
}
OrbitControls feels unresponsive, or does not move at all
If enableDamping is set to true, the controls need controls.update() called every frame inside the render loop to process the damping. Forgetting this call is the most common cause of OrbitControls appearing broken.
Still Stuck?
This page covers the early build, up to the App class. For problems in later lessons, check the common mistake notes on each lesson page and read the error in the browser console.
