Uncaught ReferenceError: THREE is not defined

Hi, I’m brand new to three.js and javascript and I can’t figure out why I’m getting errors.

Uncaught ReferenceError: THREE is not defined

file: …/index.html:19


Loading failed for the module with source “file:…/build/three.module.js”.

What is the correct way to include convexgeometry.js and three.js?
Thanks :slight_smile:

<!DOCTYPE html>

        <meta charset="utf-8">
            body { margin: 0; }
            canvas { width: 100%; height: 100% }
        <script type="module" src="src/three.js"></script>
        <script type="module" src="jsm/geometries/ConvexGeometry.js"></script>

            var scene = new THREE.Scene();

            var webGLRenderer = new THREE.WebGLRenderer();
            webGLRenderer.setClearColor(0xdddddd, 1.0);
            webGLRenderer.setSize(window.innerWidth, window.innerHeight);
            webGLRenderer.shadowMapEnabled = true;

            var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
            webGLRenderer.setSize(window.innerWidth, window.innerHeight);

            camera.position.x = 0;
            camera.position.y = 0;
            camera.position.z = -90;

            camera.lookAt(new THREE.Vector3(0, 0, 0));

            var ambientLight = new THREE.AmbientLight(0xffffff);

            var robot = new THREE.Object3D();

            var cubeVertices = [];

            cubeVertices.push(new THREE.Vector3(-10, -10, -10));
            cubeVertices.push(new THREE.Vector3(10, -10, -10));
            cubeVertices.push(new THREE.Vector3(-10, -10, 10));
            cubeVertices.push(new THREE.Vector3(10, -10, 10));

            var cubeGeo = new ConvexGeometry(cubeVertices);

            var cubeMesh = new THREE.Mesh(cubeGeo, new THREE.MeshBasicMaterial({color: 0x0000ff}));
            function render() {

                 webGLRenderer.render(scene, camera);


You are mixing ES6 module code with global script usage. Try it like so:

<script src="src/three.js"></script>
<script src="js/geometries/ConvexGeometry.js"></script>

Notice that ConvexGeometry is included from js not jsm.

These changes result in
Uncaught SyntaxError: import declarations may only appear at top level of a module [three.js:1](file:/src/three.js)

Uncaught SyntaxError: import declarations may only appear at top level of a module [ConvexGeometry.js:1](file:/jsm/geometries/ConvexGeometry.js)

Uncaught ReferenceError: THREE is not defined




Which is why I added module to both as it seemed to get rid of the error.

Also, the files have convexgeometry in jsm
My folder looks like this (I don’t know what I’m doing I’m just trying to make a basic robot)
I got the files from here Releases · mrdoob/three.js · GitHub

In this case, the beginner example may be helpful.

See BeginnerExample at * discourse.threejs.hofk.de

2021-03-16 17.05.23

from the Collection of examples from discourse.threejs.org

This is helpful, and I was looking for something like this, however I don’t see any ConvexGeometry examples, and I don’t see how my include of ConvexGeometry is any different than the includes in some of this code (after removing module). The main difference seems to be in the structure, as they didn’t put the script in the body. Surely it can’t be this complicated?




Sorry, I missed that. Using the code example I was able to get it to work! Thanks for the quick help! It’s greatly appreciated!

You are welcome. :beers: