Steps that I followed:
-Downloaded three.js master from github. Imported to project root as “threejs/”
-Index.html (at root):
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>3D Demo</title>
<script type="module" src="index.js"></script>
</head>
<body>
</body>
</html>
-index.js (also at root):
import * as THREE from "./threejs/build/three.module.js";
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( 1, 1, 1 );
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
const cube = new THREE.Mesh( geometry, material );
scene.add( cube );
camera.position.z = 5;
function Update() {
requestAnimationFrame( Update );
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render( scene, camera );
};
Update();
-Test, and it works. However, I when I try to also import either OrbitControls or GLTFLoader I get the error:
Uncaught TypeError: Error resolving module specifier “three”. Relative module specifiers must start with “./”, “../” or “/”.
coming from either of those two files at the line where they [import {...} from "three"
]
How do I fix this without importing from CDN?
(I tried using the cdn version but I had similar trouble there, even after using an import map as suggested).
I have spent many many hours trying to figure this out with no avail. I am extremely thankful to whoever helps.