# Import / Export Modules in Babylon

**URL:** <https://forum.babylonjs.com/t/import-export-modules-in-babylon/33575>\
**Category:** Questions\
**Created:** [September 1, 2022, 9:40am UTC](https://forum.babylonjs.com/t/import-export-modules-in-babylon/33575 "2022-09-01T09:40:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SamuelSousa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/samuelsousa/32/33388_2.png) [@SamuelSousa](https://forum.babylonjs.com/u/SamuelSousa)\
**Post date:** [September 1, 2022, 9:40am UTC](https://forum.babylonjs.com/t/import-export-modules-in-babylon/33575/1 "2022-09-01T09:40:44Z")

</div>

Hi Community!

I’m trying to separate my Babylon project through several files.  
The way I would like to do this is through the use of Javascript modules.

I would like to do “export function name(){}”  
and "import { name } from ‘module.js’ " onto my “MyBabylonGame.js” file

I have tried to run the “basic” Babylon script in a module (“script type=module”) from the HTML

This:

> var canvas = document.getElementById("renderCanvas");
> 
> ```
> var startRenderLoop = function (engine, canvas) {
> engine.runRenderLoop(function () {
> if (sceneToRender && sceneToRender.activeCamera) { sceneToRender.render();} });
> }
> 
> var engine = null;
> var scene = null;
> var sceneToRender = null;
> var createDefaultEngine = function() { 
> return new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true, disableWebGL2Support: false}); 
> };
> var createScene = function () {
> 
> var scene = new BABYLON.Scene(engine);
> var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
> camera.setTarget(BABYLON.Vector3.Zero());
> camera.attachControl(canvas, true);
> var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
> light.intensity = 0.7;
> var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 2, segments: 32}, scene);
> sphere.position.y = 1;
> var ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 6, height: 6}, scene);
> 
> ```

> // CALL IMPORTED FUNCTION THAT DOES EVERYTHING ELSE HERE

> ```
> return scene;
> };
> 
> window.initFunction = async function() {    
> var asyncEngineCreation = async function() {
> try { return createDefaultEngine();} 
> catch(e) {
> console.log("the available createEngine function failed. Creating the default engine instead");
> return createDefaultEngine(); }
> }
> 
> window.engine = await asyncEngineCreation();
> if (!engine) throw 'engine should not be null.';
> startRenderLoop(engine, canvas);
> window.scene = createScene();};
> initFunction().then(() => {sceneToRender = scene                    
> });
> 
> // Resize
> window.addEventListener("resize", function () {
> engine.resize();
> });
> </script>
> 
> ```

but that gives an ERROR: “Uncaught (in promise) engine should not be null”.

I have no idea what to try next…

Has any one done this? Is there a tutorial I could take a look at?

I’ve seen this Playground from another similar topic (Works fine but not what I want (at least for now 🙂 )) :

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#WF3VKZ)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<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:** [September 1, 2022, 10:36am UTC](https://forum.babylonjs.com/t/import-export-modules-in-babylon/33575/2 "2022-09-01T10:36:25Z")

</div>

You are using a UMD version as a module script. We are currently not (yet!) supporting module loading directly in the browser, but it is coming soon. At the moment you will still need to use the UMD way, or using global namespacing (the BABYLON namespace). If you are using a packer (like webpack,rollup) use the es6 (@babylonjs/???) packages instead of the UMD packages and then you can import anything you want

---

<div class="post-metadata">

**Author:** ![SamuelSousa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/samuelsousa/32/33388_2.png) [@SamuelSousa](https://forum.babylonjs.com/u/SamuelSousa)\
**Post date:** [September 1, 2022, 2:04pm UTC](https://forum.babylonjs.com/t/import-export-modules-in-babylon/33575/3 "2022-09-01T14:04:52Z")

</div>

Hi!

Thanks for taking the time and answering out clear.  
I’ll have a look into these other options.

Thanks.
