Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
I don't know why this three.js example doesn't work

I installed three.js through npm with the command: npm install --save three
I'm currently using VS-Code & have two files : index.html and index.js By copying a simple example from threejs.org I can't seem to get three.js to work,because i only get a blank screen after running a live server,instead of a rotating cube.

Here's the code to both files:

import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 
0.1, 1000 );

const renderer = new THREE.WebGLRenderer();

renderer.setSize( window.innerWidth, window.innerHeight );

document.body.appendChild( renderer.domElement );

const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
const cube = new THREE.Mesh( geometry, material);
scene.add(cube);

camera.position.z = 5;

function animate() {
requestAnimationFrame(animate)

cube.rotation.x += 0.01;
cube.rotation.y += 0.01;

renderer.render( scene, camera )
}
animate();
 <!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet" href = "index.css">
<script src = "index.js"></script>
<title >Title</title>
</head>

<body style = "margin: 0px;">


</body>
</html>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you open the page in a browser and take a look at the dev tools menu, you'll find the following error:enter image description here.

If you modify the script tag with type="module" to resolve this error, you get a new error: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../".

Basically, the browser isn't able to find the three.js JavaScript library.

One easy fix is to just tell the browser to load the library from a web url when the page loads instead of installing it through npm and then loading those installed npm files into the web page:

<!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet" href = "index.css">
<!-- This script tag loads the three.js library via url -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<title >Title</title>
</head>

<body style = "margin: 0px;">
<script src = "index.js"></script>
</body>
</html>
// import is no longer needed since the added script tag loads the library and adds the global THREE variable into the page
// import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 
0.1, 1000 );

const renderer = new THREE.WebGLRenderer();

renderer.setSize( window.innerWidth, window.innerHeight );

document.body.appendChild( renderer.domElement );

const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
const cube = new THREE.Mesh( geometry, material);
scene.add(cube);

camera.position.z = 5;

function animate() {
  requestAnimationFrame(animate)

  cube.rotation.x += 0.01;
  cube.rotation.y += 0.01;

  renderer.render( scene, camera )
}
animate();

Also note that document.body.appendChild will only work once the document has loaded (which doesn't necessarily happen before your script is executed). This issue was fixed by adding your <script> tag below the <body> tag in the html.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!