Trying to use babylon.js with Typescript on NestJS server

import {Scene, NullEngine} from '@babylonjs/core';

simulateGame() {
    const engine = new NullEngine();
    const scene = new Scene(engine);
  }

I can’t make the NestJS server start anymore.

I keep on getting:

@babylonjs/core/index.js:1
export * from "./abstractScene";
^^^^^^

SyntaxError: Unexpected token export
    at Module._compile (internal/modules/cjs/loader.js:721:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)

My tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  },
  "exclude": ["node_modules", "dist"]
}

I’m pretty sure this is a node + ts issue but i can’t figure it out.
Does anyone have an idea what might be the issue?
I tried importing with require as well.

pinging @sebavan, the keeper of the Deep Dark Secrets of the Modules

i am totally not sure you can use “module”: “commonjs”, by relying on our es version of Babylon.

In your case it might be simpler to rely on the babylonjs package ?

1 Like

is it really the only way to use babylonjs instead of babylon/core ?
isn’t there a way to work around it ?

Hey @M0D1,
It is possible to use es modules on the server

It should work with the latest node versions without trouble ?

According to this blog post
CommonJS vs. ES modules in Node.js - LogRocket Blog.
The support was introduced in NodeJS v8.50 but the stable support is from the v13.2.0 so it looks like it’s out there already for a while.
I’m using node v16.13 and I don’t have any troubles with that setup.

1 Like