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

**URL:** <https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214>\
**Category:** Questions\
**Created:** [February 7, 2021, 12:29am UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214 "2021-02-07T00:29:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr.sudo](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@mr.sudo](https://forum.babylonjs.com/u/mr.sudo)\
**Post date:** [February 7, 2021, 12:29am UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214/1 "2021-02-07T00:29:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [February 8, 2021, 4:11pm UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214/2 "2021-02-08T16:11:56Z")

</div>

pinging @sebavan or @RaananW

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [February 8, 2021, 4:26pm UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214/3 "2021-02-08T16:26:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [February 8, 2021, 4:29pm UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214/4 "2021-02-08T16:29:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mr.sudo](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@mr.sudo](https://forum.babylonjs.com/u/mr.sudo)\
**Post date:** [February 10, 2021, 7:09pm UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214/5 "2021-02-10T19:09:37Z")

</div>

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:

 ![Screen Shot 2021-02-10 at 11.58.22 AM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/e/e44c176253bbbb35643133ff50bb406c3bc40174.jpeg)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [February 10, 2021, 8:11pm UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214/6 "2021-02-10T20:11:37Z")

</div>

I do not understand how you can see this:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/c/cd9e132a413f8bb9aa58973c92f73754da062d43.png)

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

---

<div class="post-metadata">

**Author:** ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)\
**Post date:** [February 11, 2021, 9:21am UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214/7 "2021-02-11T09:21:10Z")

</div>

Something is completely wrong with our packages:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/3/3864ea44a91da072fb5f67d335217cc2b4f47ea8.png)

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

---

<div class="post-metadata">

**Author:** ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)\
**Post date:** [February 11, 2021, 9:26am UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214/8 "2021-02-11T09:26:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mr.sudo](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@mr.sudo](https://forum.babylonjs.com/u/mr.sudo)\
**Post date:** [February 11, 2021, 8:18pm UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214/9 "2021-02-11T20:18:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [February 11, 2021, 8:39pm UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214/10 "2021-02-11T20:39:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)\
**Post date:** [February 12, 2021, 8:20am UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214/11 "2021-02-12T08:20:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mr.sudo](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@mr.sudo](https://forum.babylonjs.com/u/mr.sudo)\
**Post date:** [February 16, 2021, 5:04pm UTC](https://forum.babylonjs.com/t/npm-cannot-import-as-babylon-from-babylonjs-core-legacy-legacy/18214/12 "2021-02-16T17:04:40Z")

</div>

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!
