I’m trying to use the newer ES6 modules and import only what I need in my ts files but I get an error that I don’t understand.
I used to import everything in the following way: import * as BABYLON from 'babylonjs';
So I try to change this to only the things I need like so: import { Vector3 } from "@babylonjs/core/Maths/math.vector";
And I get the followng error: Error: require() of ES Module C:\Users\emiel.…\Game\Server\node_modules@babylonjs\core\Maths\math.vector.js from C:\Users\emiel.…\Game\Server\src\rooms\schema\RaiderRoomState.ts not supported. Instead change the require of math.vector.js in C:\Users\emiel.…\Game\Server\src\rooms\schema\RaiderRoomState.ts to a dynamic import() which is available in all CommonJS modules.
As I’m not using required() anywhere in my code, I figure Babylon does it on its own? But I can’t find the reason why, or how to fix this.
I’ve tried changing the import to import("@babylonjs/core/Maths/math.vector");
But the error persists…
Any tips or guidance is welcom.
FYI total noob here, so try to ELI5? Any documentation is also appreciated!
import { Vector3, Color3 } from ‘@babylonjs/core/Maths/math’
All of that said, I found using es6 imports of Babylon libraries to be more trouble than it’s worth. So I went back to including the CDN library in the header of the HTML.
Thanks for the reply. I’m 100% certain there is no require statement directly in the code. I don’t get the error if I don’t use Vector3 in the file though.
This code uses Vector3 and results in the error.
setPositionFromStartingPosition(): void {
if (this.x && this.y && this.z && this.mesh) {
this.mesh.position = new Vector3(this.x, this.y, this.z);
}
}
Any other possibilities maybe?
Thanks for the link to the other thread. So you’re saying it isn’t worthwhile treeshaking the modules you need?
Can you Control+click on the "@babylonjs/core/Maths/math.vector" part? It should navigate you to node_modules/@babylonjs/core/Maths/math.vector.d.ts. This is a sanity check TS language server works.
Another thing to try, rm -rf node_modules && npm install (or the Windows equivalent).
Yet another, as @bigrig hinted by from ‘@babylonjs/core/Maths/math’, try to import from more close-to-the-root path, go ahead directly with @babylonjs/core, it works for me. Properly working tree shaking should still include only the code you use.
And for me using ES6 modules is definitely worth it, I like to see an explicit list of my dependencies at the beginning of the file where the imports are. That’s not obvious if you just use the BABYLON namespace.
Here’s a file example from a semi-abandoned project of mine, a scripting engine for BabylonJS, that relies solely on ES6 imports.
Yea, for me wasnt worth while b/c i need almost every library - so i was only saving a meg or so - and the compile time was killing me. If you’re only using a few libraries, i could see it being more useful.