Sign in to follow this  
DylanD

BabylonJs development in TypeScript with npm, AdcancedDynamicTexture undefined

Recommended Posts

So I had made a scene in javascript and am now putting it all into typescript.  

I am trying to use BABYLON.AdvancedDynamicTexture.CreateFullscreenUI("UI");

However I was getting an error: "Error: Cannot read property 'AdvancedDynamicTexture' of undefined"

It seems that this was a common problem up until a fix last September, this fix:  https://github.com/BabylonJS/Babylon.js/issues/2569

I have tried putting this in all my .ts files

import * as BABYLON from "babylonjs";

import * as BABYLON from "babylonjs-gui"

 with tsconfig.json:

 

"compilerOptions": {
"target": "ES6",
"module": "system",
"moduleResolution": "node",
"outDir": "js/build",
"rootDir": "./ts",
"noEmitOnError": true,
"types": [
"babylonjs",
"babylonjs-gui"
],
},

But, it gave me a new error much more generic:

Error: fetch error: 404 Not found

    instantiating http://localhost:3000/babylonjs

           loading http://localhost:3000/js/build/game.js

                 loading app

                       at fetch.js:37

 

 

so I went to the build folder and into the game.js and sure enough the first line is 

/// <reference path="../node_modules/babylonjs/babylon.d.ts" />

this line then shows the error [js] File '../node_modules/babylonjs/babylon.d.ts' not found.

any ideas what to do?

Just noticed this happens even if I don't use Babylon Gui

Share this post


Link to post
Share on other sites

Howdy,

I actually deployed an example of how to achieve that yesterday - http://doc.babylonjs.com/features/npm_support#example-of-a-webpack-project-using-babylon-modules

 

You cannot import to the same namespace. the AdvancedDynamicTexture is a part of the GUI package. you could do either:

 

import * as BABYLON from "babylonjs";

import * as GUI from "babylonjs-gui";

GUI.AdvancedDynamicTexture.CreateFullscreenUI("ui1");

 or the better way IMO:

import { AdvancedDynamicTexture } from "babylonjs-gui";

AdvancedDynamicTexture.CreateFullscreenUI("ui1");

As i dont know yout project setup, i cannot comment regarding the 404, but make sure you have both packages installed (no need to import typings, your IDE will do it for you), and that you import the namespaces correctly

Share this post


Link to post
Share on other sites
4 hours ago, RaananW said:

Howdy,

I actually deployed an example of how to achieve that yesterday - http://doc.babylonjs.com/features/npm_support#example-of-a-webpack-project-using-babylon-modules

 

You cannot import to the same namespace. the AdvancedDynamicTexture is a part of the GUI package. you could do either:

 


import * as BABYLON from "babylonjs";

import * as GUI from "babylonjs-gui";

GUI.AdvancedDynamicTexture.CreateFullscreenUI("ui1");

 or the better way IMO:


import { AdvancedDynamicTexture } from "babylonjs-gui";

AdvancedDynamicTexture.CreateFullscreenUI("ui1");

As i dont know yout project setup, i cannot comment regarding the 404, but make sure you have both packages installed (no need to import typings, your IDE will do it for you), and that you import the namespaces correctly

Hey!  Thanks I will try this and get back to you!

 

 

Also thanks DeltaKosh for pinging.

Share this post


Link to post
Share on other sites

@RaananW Hello again.  Sorry as I'm not very well versed in web development... yet.  However is this a typo or did I miss something?

"

 

We will also add an html file with a canvas (index.html):

 

<!DOCTYPE html> <html> <head> <style> html, body { overflow: hidden; width: 100%; height: 100%; margin: 0; padding: 0; text-align: center; } #renderCanvas { width: 100%; height: 100%; touch-action: none; } </style> </head> <body> <canvas id="renderCanvas"></canvas> <script src="dist/index.js"></script> </body> </html>

 

After adding index.ts we are ready to start developing.

"

You say "we will also add an html file with a canvas(index.html)", so I made a file and saved it as index.html, then immediately after you say "after adding index.ts we are ready to start developing".

Should I have saved the html file as a ts, or maybe should I have made my own ts folder?

 

Any ideas?

Share this post


Link to post
Share on other sites

Update:

I can't seem to get around "mode: "development" ", if I run npm run build then it gives me unexpected identifier error, if I add it to modules I get invalid configuration object, if I comment it out I get many errors one saying mode is not define, and if I add its own module.exports it underlines modules as an error. 

 

Any ideas?

Sorry I need so much assistance.

Share this post


Link to post
Share on other sites

Hey Dylan,

this is actually a perfect test for the tutorial. I see now that the webpack code is missing a comma before the mode. it should be:

const path = require("path");

module.exports = {
    entry: './index.ts',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    resolve: {
        extensions: [".ts"]
    },
    module: {
        rules: [
            { test: /\.tsx?$/, loader: "ts-loader" }
        ]
    },
    mode: "development"
};

Would be great to know if you got stuck on any other step, so i can fix it

 

Share this post


Link to post
Share on other sites

Yea I will definitely let you know if I run into any other problems!  

 

Speaking of that I just added the comma, and now I get a "module.exports = {        unexpected identifier error"

should I have some kind of base  then add your web packcode on top of that?

Thanks Raanan!

Share this post


Link to post
Share on other sites

whoops yea I just copy pasted over it... :D  

Successful build many warnings(only 3 actually), to be expected though, all of them were packages, cannon oimo etc.

Still no index.js though.  Heres what I've got:

File Heirarchy:

babylon-webpackTest(

dist(

bundle.js

index.d.ts

)

node_modules()

index.html

index.ts

package-lock.json

package.json

tsconig.json

web pack.config.ts

)

 

Build Warnings and other text that came when I built it(npm run build with "build": "webpack"):


     Asset      Size  Chunks             Chunk Names
 bundle.js  1.81 MiB    main  [emitted]  main
index.d.ts  11 bytes          [emitted]
[./index.ts] 755 bytes {main} [built]
[./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 489 bytes {main} [built]
    + 1 hidden module

WARNING in ./node_modules/babylonjs/babylon.js
Module not found: Error: Can't resolve 'cannon' in '/node_modules/babylonjs'
 @ ./node_modules/babylonjs/babylon.js
 @ ./index.ts

WARNING in ./node_modules/babylonjs/babylon.js
Module not found: Error: Can't resolve 'earcut' in '/node_modules/babylonjs'
 @ ./node_modules/babylonjs/babylon.js
 @ ./index.ts

WARNING in ./node_modules/babylonjs/babylon.js
Module not found: Error: Can't resolve 'oimo' in '/node_modules/babylonjs'
 @ ./node_modules/babylonjs/babylon.js
 @ ./index.ts

So its not creating an index.js I think when it builds right?

Any ideas?

Share this post


Link to post
Share on other sites

Hey, sorry, I no longer need the web pack build, so I won't be able to work on it unless on my personal time so if I ever get to finish I will let you know how it goes.

But yes I did have a bundle.js file in the dist folder if I remember correctly.  That probably would have fixed my last issue.

Thanks for your time I hope I can get back to you on this.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.