Melancholix

Unable to import babylon

Recommended Posts

Hi, 

I am trying to import BABYLON, but TypeScript is complaining that it 'cannot find namespace BABYLON',

As a result, I cannot compile my files with gulp.

I have tried everything to get this to work, and have spent the last two days googling, to no avail.

I have tried:

import * as BABYLON from './babylon';
import * as BABYLON from './babylon.module';
import { BABYLON } from './babylon';
import { BABYLON } from './babylon.module';
import './babylon';
import './babylon.module';

None of them work.

I have attached a screenshot of the error I am getting, and my folder setup.

Hopefully someone could explain to me how to get this to work, thanks in advance!

cannot_find_namespace.png

folder_setup.png

Share this post


Link to post
Share on other sites

I'm not using typescript, but I added both babylon & canon as dependencies via npm.

image.png.507a2f0c199761db2c78bff99ea156f1.png

Then in my scene file, i have at the top:
 

import BABYLON from 'babylonjs'
import CANNON from 'cannon'
window.CANNON = CANNON

Cannon is hoisted on window, as babylon expects it to be global scope. If you have your code babylon code scattered among multiple files, import babylon in each separate file. Just import cannon the one time, at the earliest point in your tree that you can.

 

Share this post


Link to post
Share on other sites

I think you will want to do:
import * as BABYLON from 'babylonjs'

Import BABYLON from 'babylonjs'  gets only the default export.

Make sure you have alpha3.4 of babylonjs, which is only a few days old.  The from part or the import will either reference a file in your project (ie: ./file.ts or ../../file.ts) or the name of an NPM module.  ie: 'babylonjs'.  Getting started is always a struggle!

Share this post


Link to post
Share on other sites

Yep, as @brianzinn said, starting 3.1.0-alpha3.4 npm works a bit differently. I am writing the documentation right now.

And the CANNON trick from above is wonderful. this is probably the best way to integrate cannon with babylon without changing babylon's architecture (which we won't right now).

Share this post


Link to post
Share on other sites

All,

Can we expand the Examples in the doc page for 'npm_support' to include require() structures?

 

I am building my app inside of Electron, which does not support ES6 import statements without Babel and etc.  I'd like to avoid that, if at all possible, and ensure that the require()s will work just fine.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.