3d model error, GLTFLoader has already been "declared", cant find a fix

Please help, my 3d model from sketchfab wont show up and i keep getting a console error that my GLTFLoader has been declared
here is my html and my gltfloader.js


Screenshot 2024-07-19 001507

Why are you writing GLTFLoader manually? (Also that assumption doesn’t make much sense, FileLoader will load either JSON, text or array buffer. It will not parse the GLTF itself, as that’s not a format recognised by any browser API.)

Consider deleting your GLTFLoader file and import from ā€˜three/examples/jsm/loaders/GLTFLoader’ instead.

Apologies, i’m pretty new to coding and am unsure of what I’m doing.
the GLTFLoader.js is from a Github file and so is the three.js file I have. So I should get rid of the GLTFLoader and write something else in the three.js file? Sorry for the confusion as you can I’m pretty new, I just want to have a 3D model on a live sever page.

1 Like

Yup, see this example for example :ok_hand: (all examples in docs have source code in the bottom-right.)

three’s install instructions: three.js docs

you wouldn’t want to copy threejs from github or anywhere, you npm install it. there’s also no need to edit index.html. you cannot run html without a server, you wouldn’t be able to load gltf files due to browser security. all of this is taken care of if you follow the docs.

you must have node installed. open your shell, type

npm create vite
# pick javascript
# pick a project name
cd projectName
npm install three
npm run dev

open your editor. and the browser with the url the shell gave you. any edit you make to the files will be reflected in the browser. you can now edit main.js and write your code:

import * as THREE from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
...

i did everything you said and i followed all of the install instructions just now.
when i did npm run dev it gave me this error:

npm error Missing script: ā€œdevā€
npm error
npm error To see a list of scripts, run:
npm error npm run

npm error A complete log of this run can be found in: …

after following the installation instructions, here is what my main.js and my index look like:


i really just want to import models/shiba/scene from sketchfab so i can open it and use it for a class project. im so confused on how to get it to load so if someone could give me an example from here it would be much appreciated

i just now also changed the first few lines of my main.js code to:

import * as THREE from ā€˜three’;

import { OrbitControls } from ā€˜three/addons/controls/OrbitControls.js’;

import { GLTFLoader } from ā€˜three/addons/loaders/GLTFLoader.js’;

i think you must have forgot to cd into the project folder. npm create vite creates a folder that has node_modules inside and a package.json, as well as html, css and the main js files. the commands only work when you’re inside that folder. all you need to start your site is npm run dev.

imo you should not be using import maps, especially as a beginner. you can delete that section or better delete the whole folder and run npm create vite again. better don’t touch the html files that vite has created, they work out of the box without further modifications.
to explain, imports maps is an unfinished, half-broken browser spec. it poses immense manual complications and bars you from parts of the module eco system. unfortunately it does not look good, they have made some bad decisions that prevents the spec from becoming a standard.

@drcmda is correct that you do not need import maps when using a bundler, however the following statement is misleading…

This feels like an outdated and biased opinion not actually grounded in the progress of import maps specification…

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap

The good news is that modern browsers have started to support module functionality natively, and this is what this article is all about. This can only be a good thing — browsers can optimize loading of modules, making it more efficient than having to use a library and do all of that extra client-side processing and extra round trips.

Here’s a great article on the benefits of using import maps…

It hints at a future where reliance on custom framework implementations diminishes in favor of native browser features, making development smoother and more intuitive.

@drcmda do you have some resources to show why, in your opinion, it’s half-broken?