Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics

Using Multiple Files

A topic by Hedges created Apr 27, 2021 Views: 104 Replies: 2
Viewing posts 1 to 3
(1 edit)

My main.js file is getting pretty big, is there a way to split this into multiple files? I tried exporting and importing modules but this caused an "Uncaught ReferenceError: a is not defined" error.

Update: Error was fixed by adding const to the forloop on line 954. See post below for more details.


Probably the easiest stuff to break out are the UI functions from the end of the file. Let me have a think about how to split it up.

(1 edit)


I was able to fix the "Uncaught ReferenceError: a is not defined" message by adding 'const' to the forloop on line 954. So it's now, for (const a in attrs). type="module" can now be added to the main.js <script> tag and static imports work. Using dynamic imports is no longer needed. I'll add a new pastebin example.

Original Post:

I finally found a working solution using dynamic imports. For some reason adding type="module" to <script src="main.js"... breaks at line 954. Using dynamic imports skips the need for type="module" and allows imported modules to be used. All you need to do is all 'async' to the IIFE in main.js then dynamically import the modules. I have added a simple pastebin example moving 'gametitle' to another file.

I don't know the limitations of this solution, but it has been working so far. I was able to create a sound.js file and move the sfx constant into it.