NPM Cannot import * as BABYLON from '@babylonjs/core/Legacy/legacy';

Hey all, I’m using npm to add BabylonJS to my NodeJS / Express project and am having trouble importing the package into my main javascript file. I’m fairly new to web dev so perhaps the solution is something simple I’m missing. The steps I’ve taken are:

1. I used express-generator to quickly create a NodeJS / express skeleton without a specialized view renderer.
npx express-generator --no-view

After this initial setup my project structure is:

MyProject
|—app.js
|—Bin
|—node_modules
|—package-lock.json
|—package.json
|—public
|—|---index.html
|—|---index.js
|—|---style.css
|— routes

2. To add BabylonJS to the project I execute:
npm install @babylonjs/core --save

3. At the top of my javascript project file I import BabylonJS using:
import * as BABYLON from ‘@babylonjs/core/Legacy/legacy’;

4. I run the NodeJS server:
npm start

5. When I access the page at localhost:3000, the console displays the following error:
Cannot use import statement outside a module

After searching for solutions online, I found that potentially adding “type”: “module” to package.lock could help resolve this issue, but when doing so an error is thrown when I attempt to start the server, leading me to believe that this causes other parts of the project to break.

pinging @sebavan or @RaananW

Could you try not importing as BABYLON but replacing with another variable names ?

This would prevent conflict with some of the tricks used internally for the UMD packages.

Basically, legacy/ … is normally reserved for us at build time to generate the UMD version.

Also your error suggest that you are not using a transpiler and our ES6 packages contain ES6 import statement not compatible with all version of nodes and usually not with .js extensions. So you would need to use babel or such to transpile imports statements to require.

The easiest in your case as you seem to be willing to use all of Babylon might be to use the UMD package npm install babylonjs

Thank you for your help with this. No luck with replacing BABYLON with another variable name, however I have revisited installing the babylonjs package. One of the reasons I looked at using @babylonjs/core previously was because of the following error I receive when trying npm install babylonjs.

Right now, when I run npm install babylonjs the following errors occur:

I do not understand how you can see this:

It looks like what we do locally and I have never seen it from npm @RaananW ?

Something is completely wrong with our packages:

that’s from npm.js. the latest version of babylonjs is taken directly from our main directory on github. That was 9 days ago. We will fix that asap

In the meantime, please use version 4.2.0 directly if you want to use the latest stable, or “preview” if you want the latest preview version

Sounds great, I decided to use the preview version in the meanwhile, although I do still encounter the “Cannot use import statement outside a module” error when trying to use the imported package.

As @sebavan suggested, this may be resolved with the support of a transpiler, however my understanding is that node version (v14.15.1) should have support for ES6 import statements. Is there a step I missing between the following actions to get things going?

1. Add BabylonJS to the project with npm:
npm install babylonjs@preview --save

2. Add an import statement to the main javascript project file:
import * as BABYLON from ‘babylonjs’;

3. Run the NodeJS server:
npm start

4. After visiting the page at localhost:3000, the console continues to display the error:
Cannot use import statement outside a module

I think it only works from .mjs files ? or if you use typescript you could target import in es5 style ?

And - can you import anything else, or is it just a Babylon.js issue?

From what @sebavan suggested earlier, it looks like the solution to this would be to use a compiler. In the end, I decided to use the cdn version of BabylonJS and things are working great! thank you for your support!

2 Likes