I finished my project on my computer and it runs on my local host. After I uploaded it in GitHub website, the page can not load three js module and other modules I tried to import in js file, showing error 404.
import * as THREE from './node_modules/three/build/three.module.js';
import { OrbitControls } from './node_modules/three/examples/jsm/controls/OrbitControls.js';
import { GLTFLoader } from './node_modules/three/examples/jsm/loaders/GLTFLoader.js';
import {SkeletonUtils} from './node_modules/three/examples/jsm/utils/SkeletonUtils.js';
I tried different path file in the js file(including “. . /” “. . /main/”…), but still the page can not load three, orbit control, gltf loader, skeleton util. I don’t have to do all these steps in my local host, not really sure why it’s so hard to point to files on github pages.
Since you are in a subdirectory of you site you should remove ./ Or / or …/ anything
Do it like this have a try
import * as THREE from 'node_modules/three/build/three.module.js';
import { OrbitControls } from 'node_modules/three/examples/jsm/controls/OrbitControls.js';
import { GLTFLoader } from 'node_modules/three/examples/jsm/loaders/GLTFLoader.js';
import {SkeletonUtils} from 'node_modules/three/examples/jsm/utils/SkeletonUtils.js';
import * as THREE from '../build/three.module.js';
import { OrbitControls } from '/build/OrbitControls.js';
import { GLTFLoader } from '/build/GLTFLoader.js';
import {SkeletonUtils} from '/build/SkeletonUtils.js';
I created a separate folder and had to name it ‘build’ so that modules can be imported correctly. I’m not even sure why it works this way but it does
You can never import from node modules directly, it would also make very little sense to copy stuff to a build folder. Just use three as it is written in the docs. In order to consume npm you need a build tool, like vite. It has something called a resolver, it knows what “three” is and finds it in your node modules, it also tries to only include the code that is actually used in your end bundle. Node modules are for local dev only, they are never uploaded anywhere or directly accessed by code.