Importing GLTF Loader causes a white screen

Whenever I uncomment the GLTFLoader import line, my website produces a white screen

import * as THREE from './js/three/build/three.module.js';
import { GLTFLoader } from './js/three/examples/jsm/loaders/GLTFLoader.js';

const scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000);
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

const light = new THREE.DirectionalLight( 0xffffff, 1 );
light.position.set( 1, 1, 1 );
scene.add( light );

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)

Do you see any warnings or errors in the browser console?

Uncaught TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../".

It seems your application is missing a correct usage of an import map which means bare module specifier can’t be handled. I suggest you have a look at one of the official examples to see how it looks like: three.js examples

It does not matter what example your are picking. They all have one above the actual script section.

1 Like

Thank you so much! It works now