How to run bjs from a server using CLI?

Thank you, node is something to set up.

Hey,

It would be great if you could share your repo.
You can check my repo for the inspiration

1 Like

Thanks!

And is it possible to run some BJS code, but locally, without a server.

Just tried, and it seems I can’t import ts files.

tried with node and npm but it is beyond me.

Ok, I will try my luck with BabylonNative, maybe it will work

Hey, I really don’t know what you are trying to achieve - why are you talking about the BabylonNative now?
To run BJS locally for a web application without the server you can just use webpack or whatever else.
You can use my repo and only packages/client part or you can use BabylonJS template:

Actually,
I have a code that I would like to run locally, using the code from BJS, to retrieve some output in order to later inject that in-game.

I need to load some local gltf files, no webserver needed, apply my code, save some relevant info on disk and that’s it.

All the glTF loader options need an url, which I won’t have, since it’s local only.

That’s why I thought that the native could be better suited for me. It is much closer to my coding environment, setting the node environment is not a really comfortable experience for me.

I want the least overhead possible.
You confirm I am wrong with using BJSNative?

Thank you so much

For locally loading gltf files you have this section in the docs:

For what you wrote already I think going with the any web Babylon template should be fine.

Thanks,

Now I followed the setup and this is where I am at:

packages.json:

{
  "name": "babylonjs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack",
    "start": "webpack-dev-server --port 8080"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babylonjs/core": "^6.35.0",
    "html-webpack-plugin": "^5.6.0",
    "ts-loader": "^9.5.1",
    "typescript": "^5.3.3",
    "webpack": "^5.89.0",
    "webpack-cli": "^5.1.4",
    "webpack-dev-server": "^4.15.1"
  },
  "dependencies": {
    "@babylonjs/loaders": "^6.35.0",
    "xhr2": "^0.2.1"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "ESNext",
    "moduleResolution": "node",
    "noResolve": false,
    "noImplicitAny": false,
    "sourceMap": true,
    "preserveConstEnums":true,
    "lib": [
        "dom",
        "es6"
    ],
    "rootDir": "src",
    "preserveSymlinks": false
  }
}

All my files are in the src folder and a call to
node_modules/webpack/bin/webpack.js ./src/my_script.ts --output-filename=bundle.js --mode=development

generate the bundle file without issue.
But running node dist/bundle.js
raises:
ReferenceError: XMLHttpRequest is not defined

So, following https://doc.babylonjs.com/setup/support/serverSide

I added global.XMLHttpRequest = require("xhr2").XMLHttpRequest; at the top of my script but nothing changes.

For info this is what the script looks like:

global.XMLHttpRequest = require("xhr2").XMLHttpRequest;
import { Scene, Engine, NullEngine, SceneLoader, Vector3 } from "@babylonjs/core";
import "@babylonjs/loaders/glTF";

export module MyModule {
        export function my_function() {

                var engine = new NullEngine();
                var scene = new Scene(engine);

                console.log("Start");
                SceneLoader.ImportMesh("", "", "file.glb", scene, (imported) => {
                                ...
                });

        console.log("final")
}

But the issue is that I don’t want to launch a browser, so when running the bundle using node I think the issues lies there.

I will modify the SceneLoader.ImportMesh to load the file from the filesystem and keep going as usual afterwards as if the file was properly downloaded. This is the only solution I see right now.

Why you’re running this like that? If you want to have production build you have it already in packages.json in scripts - npm run build.
What are you using node for?

How you want to run it?

I want to run this node script as a CLI, offline.

I spotted the “culprit” export const RequestFile. Instead of modifying the lib, my plan is now is to submit base64 data, instead of modifying a single line of code of the lib.

Sorry but I’m not following. In general if you have build your project with npm run build you have your build artifact in dist/* folder ready to be served by node/python or whatever web server.

1 Like

I don’t know how node works honestly, the only thing I know is that I succeeded at transforming it as a CLI service for my needs.

I am serious, there are many things going on that went over my head as far as node setup is involved, and I guess I totally missed the point of node, but since I won’t use it myself anytime soon ( I will be back to backend dev), I am fine with it.

Thanks anyway neu5, for your time, your help and your support.
:wave:

Do you just want to load a file from string - did I get that right? This should do the trick then: Loading GLTF models in Nodejs

Basically, I could not use XMLHttpRequest because I wanted to use it offline, without internet connection. So, was ready to modify the source code once I spotted where to modify things, but I always feel that there is a smarter way when I am about to pull the trigger. Yes there was: it was giving the sceneloader a base64 to work with. I confirmed that, reading the source code.

Again, I don’t use JS and its ecosystem and I hope I won’t for a long time. But yeah,you have it, it was the base64 and the static webpack that made it.
:slightly_smiling_face:

Sorry for this long answer, and maybe not 100% targeting your question, but this journey really tired me. :relieved:

But you can use it offline locally.
Like here

I’m not sure what you are exactly struggling with :thinking:

You are right, I had a few hours to discover a framework, do what I needed and step to something else. There are many configuration options and I couldn’t afford to read them all.
Given more time to explore/ interest to do so, I could maybe find a proper way to do it.

But this is behind me now, the code is there, and when the js dev is back, he will modify what he wants the way he wants. :slightly_smiling_face: