How to use Server side with Babylon.js

I tried. I want to use babylon.max.js in node.js.

But an error occurred. “window is not defined”
I ran the example on the official homepage, but an error occurred.
https://doc.babylonjs.com/features/nullengine

What did I do wrong? The window variable is used in babylon.max.js. So I can hardly solve it.

What I’m trying to do is trying to configure the game server on the server side. I want to use NullEngine, but as I said above, I am getting an error.

Can you give us more details about the error? which line?
nullEngine avoids calling any window related code so this may be a bug

Thanks for the answer.

I’ll show you the bug.


$ node server
/Users/paradigm/Documents/Project/PRODUCTS/game.hwanyong.com/11.Server/Engine/Graphic/Babylon.js/babylon.max.js:10
})(window, function() {
^

ReferenceError: window is not defined
at Object. (/Users/paradigm/Documents/Project/PRODUCTS/game.hwanyong.com/11.Server/Engine/Graphic/Babylon.js/babylon.max.js:10:4)
at Module._compile (internal/modules/cjs/loader.js:722:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:733:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object. (/Users/paradigm/Documents/Project/PRODUCTS/game.hwanyong.com/11.Server/server.js:1:79)
at Module._compile (internal/modules/cjs/loader.js:722:30)

If I know right, is the window object valid only on the client side?
I run the example as it is, but it is difficult to get an error.
I may not have enough knowledge of node.js, or maybe I do not know the basic way to use babylon.js on the server side (in node.js).

Is it because I mistaken the babylon.max file for another file?
It seems that babylon.max.js is definitely made to work on the client side.

ps. Things I’ve tried

  1. I’ve Googled how to use the window object in node.js.
  2. Set the window object value in babylon.max.js to an empty object (var window = {}).
    … I did a lot of other searches but it did not help.

Can you share the code you are using?

Sure! My source is the same as the example.
Example: Server Side - Babylon.js Documentation


var BABYLON = require("../../dist/preview release/babylon.max");
var LOADERS = require("../../dist/preview release/loaders/babylonjs.loaders");
global.XMLHttpRequest = require('xhr2').XMLHttpRequest;

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

var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 20, 100), scene);

var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 100, BABYLON.Vector3.Zero(), scene);

BABYLON.SceneLoader.ImportMesh("", "https://playground.babylonjs.com/scenes/", "skull.babylon", scene, function (newMeshes) {
    camera.target = newMeshes[0];

    console.log("Meshes loaded from babylon file: " + newMeshes.length);
    for (var index = 0; index < newMeshes.length; index++) {
        console.log(newMeshes[index].toString());
    }

    BABYLON.SceneLoader.ImportMesh("", "https://www.babylonjs.com/Assets/DamagedHelmet/glTF/", "DamagedHelmet.gltf", scene, function (meshes) {
        console.log("Meshes loaded from gltf file: " + meshes.length);
        for (var index = 0; index < meshes.length; index++) {
            console.log(meshes[index].toString());
        }
    });

    console.log("render started")
    engine.runRenderLoop(function() {
        scene.render();
    })
});

  1. The file name was saved as server.js.
  2. Executed with the following command. $ node server.js
  3. I installed the required npm package.

Then, as you know, an error occurs.
I did not know, so I ran “npm i window”. And modified the babylon.max.js source. On the first line I entered:
var window = require (‘window’);

Thereafter, a “Blob is undefined” error occurs.
This method was not considered a fundamental solution.

Ok I can repro…I’ll ping @sebavan as it is actually module related

Ok found it, trying to find the best workaround for Webpack missing abilities to create universal modules: https://github.com/webpack/webpack/issues/6525

You ll have it tomorrow.

1 Like

PR is up here Allow Webpacked lib to be used in node.js by sebavan · Pull Request #5976 · BabylonJS/Babylon.js · GitHub it should be available soon on NPM.

I’m also trying the first method you gave me. And as soon as PR is merged into the master, I will apply it immediately.
Thank you very much.

1 Like

Thanks everyone. I’ve run through NPM after I checked whether I merged. It works really well! @Deltakosh @sebavan

1 Like

glad that it works!