I have a small pickle with how to setup my Babylon.js server side setup using NullEngine and TypeScript. I have a skeleton of a client side of the game done and I would like to share as much code as possible running the game server side. Trying to build kind of authoritative game server here
Basically I have a Game class, in Game.ts. It sets up all the game stuff, such picking if we should use regular engine or the null engine. So the first line in this Game.ts file is this: import { Engine, NullEngine } from '@babylonjs/core'
Then I have my server.ts file, where I try to import the game with the regular import syntax. import Game from '../../common-game/src/Game'
After compiling this using tsc and trying to run with node server.js (or, just using ts-node), I get this error: export * from "./abstractScene"; ^^^^^^ SyntaxError: Unexpected token 'export'
So everything fails, but I don’t quite understand why. Could someone share light on this? How NullEngine should be utilized in Node.js environment if we want to use import statements?
If someone would be so kind to check it and point out what I’m doing wrong here…
Installation is simple if you have node and npm installed. Just:
npm install
npm run build
npm run start
You can also try the node-ts-dev one which is npm run dev. They all should give the same error though. I’m really just hoping to find out the best way to deal with the problem
Could someone from the dev team help me with this or is the question itself weird? Do you know who would be the best person to consult about this @sebavan ?
Since the error comes from Babylon.js import, I just would like to:
Figure out if this error is “working as intended”, meaning that I am trying to use NullEngine / Babylon.js server side as it is not meant to be used.
How to properly set up NullEngine TypeScript/ES6 project with imports/exports as you would recommend.
As explained earlier node-js expects common.js import style until version 14 so no import / export keywords could work before.
In order to convert our import statements, either you could rely on a bundler like webpack/parcel/rollup or you could transpile your files using babel.
Here using babel and babel-node for instance, you can add the following dependencies:
As I said earlier, I am using Node version 14+ in my project. I’m also using @babylonjs/core -package, which should be ECMAScript modules compatible. Node version 14+ support ECMAScript modules as well. I do not quite understand why I need the babel transformations, as everything should, in theory, work well server side (node.js) with import/export statements.
To further solidify my point, how is it that I can import similarly styled physics library cannon without problems, but for Babylon it can’t be done? Is there some issue when babylon esm build is generated?
Example: cannon-es/dist at master · pmndrs/cannon-es · GitHub
Edit the tsconfig.json to have "module": "ESNext" and "target": "ESNext" (something like es6 might be ok too…).
After the modifications to the project the build and start scripts work. All in all I’m pretty surprised how confusing the Node.js ecosystem is for the ESM field…
If somebody from the future is reading this, you need at least Babylon.js version 5.0.0-alpha.3 for this to work.