# How to run bjs from a server using CLI?

**URL:** <https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686>\
**Category:** Questions\
**Created:** [December 24, 2023, 2:31pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686 "2023-12-24T14:31:05Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![JabiK](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@JabiK](https://forum.babylonjs.com/u/JabiK)\
**Post date:** [December 24, 2023, 2:31pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/1 "2023-12-24T14:31:05Z")

</div>

Thank you, node is something to set up.

---

<div class="post-metadata">

**Author:** ![neu5](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/neu5/32/24720_2.png) [@neu5](https://forum.babylonjs.com/u/neu5)\
**Post date:** [December 24, 2023, 2:54pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/2 "2023-12-24T14:54:35Z")

</div>

Hey,

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

> **[GitHub - neu5/babylonjs-nullengine-havok](https://github.com/neu5/babylonjs-nullengine-havok)**
>
> Contribute to neu5/babylonjs-nullengine-havok development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![JabiK](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@JabiK](https://forum.babylonjs.com/u/JabiK)\
**Post date:** [December 24, 2023, 4:19pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/3 "2023-12-24T16:19:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JabiK](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@JabiK](https://forum.babylonjs.com/u/JabiK)\
**Post date:** [December 24, 2023, 5:12pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/4 "2023-12-24T17:12:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![neu5](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/neu5/32/24720_2.png) [@neu5](https://forum.babylonjs.com/u/neu5)\
**Post date:** [December 24, 2023, 6:16pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/5 "2023-12-24T18:16:13Z")

</div>

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:

> **[GitHub - RaananW/babylonjs-webpack-es6: Babylon.js basic scene with...](https://github.com/RaananW/babylonjs-webpack-es6)**
>
> Babylon.js basic scene with typescript, webpack, es6 modules, editorconfig, eslint, hot loading and more. Will even make coffee if you ask nicely. - GitHub - RaananW/babylonjs-webpack-es6: Babylon....

---

<div class="post-metadata">

**Author:** ![JabiK](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@JabiK](https://forum.babylonjs.com/u/JabiK)\
**Post date:** [December 24, 2023, 8:48pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/6 "2023-12-24T20:48:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![neu5](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/neu5/32/24720_2.png) [@neu5](https://forum.babylonjs.com/u/neu5)\
**Post date:** [December 25, 2023, 8:47am UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/7 "2023-12-25T08:47:36Z")

</div>

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

> **[.glTF File Loader Plugin | Babylon.js Documentation](https://doc.babylonjs.com/features/featuresDeepDive/importers/glTF#loading-files-locally)**
>
> Learn about the .glTF File Loader Plugin available in Babylon.js.

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

---

<div class="post-metadata">

**Author:** ![JabiK](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@JabiK](https://forum.babylonjs.com/u/JabiK)\
**Post date:** [December 25, 2023, 11:52am UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/8 "2023-12-25T11:52:53Z")

</div>

Thanks,

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

packages.json:

```auto
{
  "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

```auto
{
  "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](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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![neu5](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/neu5/32/24720_2.png) [@neu5](https://forum.babylonjs.com/u/neu5)\
**Post date:** [December 25, 2023, 1:45pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/9 "2023-12-25T13:45:30Z")

</div>

> [@JabiK](#):
>
> But running `node dist/bundle.js`

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?

> [@JabiK](#):
>
> 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.

How you want to run it?

---

<div class="post-metadata">

**Author:** ![JabiK](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@JabiK](https://forum.babylonjs.com/u/JabiK)\
**Post date:** [December 25, 2023, 2:08pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/10 "2023-12-25T14:08:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![neu5](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/neu5/32/24720_2.png) [@neu5](https://forum.babylonjs.com/u/neu5)\
**Post date:** [December 25, 2023, 3:35pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/11 "2023-12-25T15:35:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JabiK](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@JabiK](https://forum.babylonjs.com/u/JabiK)\
**Post date:** [December 25, 2023, 5:26pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/12 "2023-12-25T17:26:26Z")

</div>

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.  
👋

---

<div class="post-metadata">

**Author:** ![Joe\_Kerr](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/joe_kerr/32/39017_2.png) [@Joe\_Kerr](https://forum.babylonjs.com/u/Joe_Kerr)\
**Post date:** [December 25, 2023, 6:01pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/13 "2023-12-25T18:01:09Z")

</div>

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](https://forum.babylonjs.com/t/loading-gltf-models-in-nodejs/41122)

---

<div class="post-metadata">

**Author:** ![JabiK](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@JabiK](https://forum.babylonjs.com/u/JabiK)\
**Post date:** [December 25, 2023, 6:59pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/14 "2023-12-25T18:59:49Z")

</div>

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.  
🙂

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

---

<div class="post-metadata">

**Author:** ![neu5](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/neu5/32/24720_2.png) [@neu5](https://forum.babylonjs.com/u/neu5)\
**Post date:** [December 25, 2023, 8:31pm UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/15 "2023-12-25T20:31:37Z")

</div>

But you can use it offline locally.  
Like here

> **[.glTF File Loader Plugin | Babylon.js Documentation](https://doc.babylonjs.com/features/featuresDeepDive/importers/glTF#loading-files-locally)**
>
> Learn about the .glTF File Loader Plugin available in Babylon.js.

I’m not sure what you are exactly struggling with 🤔

---

<div class="post-metadata">

**Author:** ![JabiK](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@JabiK](https://forum.babylonjs.com/u/JabiK)\
**Post date:** [December 26, 2023, 7:06am UTC](https://forum.babylonjs.com/t/how-to-run-bjs-from-a-server-using-cli/46686/16 "2023-12-26T07:06:01Z")

</div>

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. 🙂
