That error is coming from OrbitControls.js – inside that file you’ll find lines such as
import {
EventDispatcher,
Vector3,
// …
} from 'three';
“three” is a bare module specifier.
A browser can’t resolve bare specifiers unless you:
- Bundle everything with something like Vite / Webpack, or
- Tell the browser what “three” means by using an import map, or
- Ask the CDN to rewrite every internal import for you (e.g. with ?module).
Because you’re running straight in the browser without a bundler, the quickest fix is to add an import map.
Put it before any script that uses the mapping:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Three.js Solar System</title>
<!-- 1️⃣ Import-map: maps the bare specifier "three" to the CDN file -->
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.160.0/build/three.module.js"
}
}
</script>
<style>
body { margin:0; overflow:hidden; background:#000; }
canvas { display:block; }
</style>
</head>
<body>
<!-- 2️⃣ Your code -->
<script type="module">
import * as THREE from 'three'; // now works 🎉
import { OrbitControls } from
'https://unpkg.com/three@0.160.0/examples/jsm/controls/OrbitControls.js';
/* ------- the rest of your script is unchanged ------- */
// --- Scene ---
const scene = new THREE.Scene();
// Camera, renderer, lights, planets, animation loop...
// (keep everything you already had)
</script>
</body>
</html>
That’s it—open the file in any modern browser and the solar-system scene should run without the “Failed to resolve module specifier 'three'” error.
━━━━━━━━━━━━━━━━━━━━
Alternative (no import map)
━━━━━━━━━━━━━━━━━━━━
If you’d rather not use import maps, append ?module to every CDN URL so unpkg rewrites the internal imports for you:
<script type="module">
import * as THREE from 'https://unpkg.com/three@0.160.0/build/three.module.js?module';
import { OrbitControls } from 'https://unpkg.com/three@0.160.0/examples/jsm/controls/OrbitControls.js?module';
Either approach will solve the problem; the import-map version tends to be cleaner and is exactly what the official three.js examples use.