I have created an index.html but its in the /examples/ folder where I currently have all my files added, and its imports are pointing to /jsm/. Like the other examples in the folder. I want to relocate my index to the build root and repoint its dependecies to the correct place. Also move my newly created files that I put in /jsm/ into the right place too.
My pointers to those files are as follows:
import { OrbitControls } from "./jsm/controls/OrbitControls.js";
Of course, in the deployed version it isnât seeing these because /examples/ isnât deployed. I need to move my index.html from /examples/ into the root and repoint my imports to the appropriate folders.
So, to do this what are the steps? I also notice that in the other folders, that are deployed, the closest match to files that were in the /examples/ child folders like /examples/jsm/ and /examples/jsm/utils/ are not there. How to move a file from /examples/ into the deployable folder? Or do I just need to repoint my imports to the root?
Iâm trying to deploy to a gitHub/hosting server and although I do get a successful build of ThreeJS /build/ there is nothing shows up on the browser because all of my files (index.html, etc.) are in the non-deployed /examples/.
Sorry for the over-explanation. I just wanted to be clear. Thank you in advance!
What bundler are you using? Webpack? Typically you would just install Three.js via Node Package Manager with npm install three and then youâd have everything you need in your projectâs node_modules folder. So if you need the core library, you could do:
import * as THREE from "three";
and any other module from the examples folder could be used like this:
import { OrbitControls} from "three/examples/jsm/OrbitControls.js";
Your own code is almost always separate from the libraries youâre using. Iâm not sure why youâre moving your index.html file into your /examples/ folder. It has no reason to be there. Are you copy and pasting the "three" folder structure?
⌠Iâm truly an idiot. I see now. I always overcomplicate the simplest things. I thought your build process was completely different than it is. Wow I feel dumb lol.
@marquizzo - Ok, so, Iâm still missing something. I thought I had it. And did, in my local, when I moved the index to the root folder. However, Iâm deploying to Netlify. They only read the âindex.htmlâ from the /build/ folder. So now build looks like this:
In the index I have my pointers to the modules like so:
import * as THREE from 'three.module.js';
import Stats from './examples/jsm/libs/stats.module.js';
import { GUI } from './examples/jsm/libs/dat.gui.module.js';
import { TWEEN } from "./examples/jsm/libs/tween.module.min.js";
import { OrbitControls } from "./examples/jsm/controls/OrbitControls.js";
However, nothing seems to point correctly. Iâve tried a few variations of the relative pointers but nothing seems to work. What am I doing wrong?
Ya, this isnât working. There is something a little weird about deploying with NPM/ThreeJS. I tried a few things from different posts. I tried using the ./node_modules/three/... approach (doesnt work either).
I am trying the CDN approach now but it seems that its a common issue to have this work properly across certain types of common build servers. Surely, one does not have to change hosting providers to find a working solution.
Netlify is a free solution. They allow deploying straight from GitHub. Iâm not promoting or advertising it but simply pointing out that to create a test environment yourself with ThreeJS and trying to create a module deployment of the ThreeJS package is fairly easy to do. Rather than this objective approach (that should work) could someone try an subjective approach and build it themselves and tell me how they got their solution to work? Ideally, I dont want to use a CDN path for obv reasons. Surely there is a way to make this work off the enclosed build files from Three.
Aside from Netlify just trying to make this work at a local build level with the index.html inside of /build/ has the same issues. I cannot get the index to find a correct pointer to three.module.js. Even when its in the same folder. I saw the post from @Mugen87 on not using Bare Includes ⌠so whatâs the solution then?
HELP!! Iâm sure this is going to be a common issue in the near future. I dont think Three nec needs to keep up with the latest trend in deployment architecture but this ones going to be really common.
Iâve deployed Three.js projects with Netlify before, and they work fine. Since you canât get it to run locally on your machine, it leads me to believe youâre just struggling with your folder structures and your bundler. I recommend you read the Installation section in the docs to get better acquainted with the setup. I donât think this is a problem that Netlify or Three.js needs to address.
See this simple demo Iâve created, itâs a standalone JS file, with no node_modules dependencies. It uses import statements to load Three.js core and modules.
import * as THREE from "https://cdn.skypack.dev/three@0.133";
import { OrbitControls } from 'https://cdn.skypack.dev/three@0.133/examples/jsm/controls/OrbitControls.js';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
not â./âŚâ which is a local/relative path. it does not matter if you publish to netlify or anything else, thatâs not the problem, youâre just messing modules up with these paths and thatâs an easy fix.
pick a bundler. any bundler. i recommend vite because it canât get any simpler than that. npm init vite name your project, pick vanilla.
cd into your new project, npm install three
npm run dev now build your app, if you save in your editor youâll see it reflected immediately in the browser
when youâre done: npm run build
move the dist folder to netlify
only #1 varies, the other steps are the same for virtually all bundlers out there, parcel, webpack, etc. though again, if you already struggle with imports, use vite because other bundlers, especially webpack, can get quite complex. youâll make it!
Great call on Vite. After learning how to do it with WebPack I agree that Vite is pretty easy. It took me about a day to really understand it and luckily my case is pretty basic. I ended using up Vite and I love it. You were absolutely right and the paths now all work as you suggested.