Home > Mobile >  Why is my installaton of ThreeJS inside of laravel not working?
Why is my installaton of ThreeJS inside of laravel not working?

Time:05-02

First of all I know Laravel is a PHP framework and has nothing to do with the frontend(JavaScript) of my application. BUT laravel provides the possibility of using webpack which if I got things right is for node modules and other javascript stuff..

What I've done so far:

Installed ThreeJS with command:

npm install three

than executed

npm run dev

After that I included the Libary in my app.js file in the resources folder of Laravel like this:

var THREE = require('three');

(I tried including it whitout assinging it to a variable but that seemed not to work so I just did what some google research told me)

Again I ran npm run dev and the Libary was included inside my packed app.js in the public/js/ path of my project. Everything seems to be fine. I included the app.js file inside of my view and also set the script attribute type to module. But now when I try to import threejs via import * as THREE from 'three';

inside my view it says that the module cannot be found. I know I could just copy the contents of ThreeJS inside the node_modules folder to my public path JS folder but I wanted to keep it as a node module. Any Advise?

CodePudding user response:

Before you can instantiate Three.Js you have to import the library:

In your resources\js\app.js

import * as THREE from 'three';

const scene = new THREE.Scene();

However, it's recommended to create a instance in the component that actually requires it unless you need it globally.

CodePudding user response:

I'm feeling like super stupid right now but adding a window.THREE instead of var three fixed the issue somehow.. Thanks for your help and time anyway!

  • Related