Errors when trying to use MeshWriter with Babylon [solved]

I’ve just installed following packages:

npm i babylonjs meshwriter

And additionally I’ve added this record to my index.html:

	<script src="libs/meshwriter.min.js"></script>

My test typescript code looks like this:

import { MeshWriter } from "meshwriter";
// import * as MeshWriter from "meshwriter";
import * as BABYLON from "babylonjs";

const engine = new BABYLON.Engine( canvas, true );
const scene = new BABYLON.Scene( engine );

const Writer2 = BABYLON.MeshWriter(scene, { scale: 1.0, defaultFont: "Arial" }); // Error: Property 'MeshWriter' does not exist on type 'typeof import("babylonjs")'

const Writer = new MeshWriter(scene, { scale: 0.1, defaultFont: "Arial" }); // Error: Uncaught TypeError: w.StandardMaterial is not a constructor
/*
More detailed:
Uncaught TypeError: w.StandardMaterial is not a constructor
    at N (meshwriter.min.js:1:6449)
    at new MeshWriter (meshwriter.min.js:1:2365)
    at onReady (index.ts:121:1)
    at HTMLDocument.<anonymous> (index.ts:132:61)
*/
const text1 = new Writer( "Blah Blah", {
	"anchor": "center",
	"letter-height": 100,
	"color": "#1C3870",
	"position": { "x": 0, "y": 0, "z": 0 },
	"letter-thickness":2,
} );

const Writer3 = ( BABYLON as any ).MeshWriter(scene, { scale: 1.0, defaultFont: "Arial" }); // index.ts:119 Uncaught TypeError: BABYLON.MeshWriter is not a function

As you see I tried 3 different approached and in both cases I have different errors. What do I do wrong?

Solved. I found the root of my problem.
In HTML file I have MeshWriter defined right after Babylonjs definition. I thought Babylonjs will detect MeshWriter definition and create a link to it inside itself.
When I reorder it vice versa then all worked fine.

2 Likes