Accessing Globals Outside NPM

I know people hate globals for good reason, maybe this is not the best way.

I have a global preference file that stores paths, and configuration settings for a project I am building using ES6, Typescript,NPM workflow. It is called config.js and is located in the script root. Both my Babylon script written in typescrript and some external 2D plan javascript libraries need to access global variables within the preference file (pathing to folders used throughout the app). I for the life of me can not get typescript to see it.

I imagine I have to tell NPM to see the globals declared in the config.js file (right now its referenced in the HTML header as an external script, so the external web libs can see it). I know babylon can’t see it like that due to compiling vs runtime building of the page. Is there a way to expose it, or include it so it can be utilized by babylon too?

I tried
require(’…/config.js’);
import * from “…/src/config.js”
declare var DEBUG: boolean // to expose a single variable as a a test.
About six other things.

Structire right now is:
root
index.html
-src
-config.js
-web library 1 Folder
–files
-web library 2 Folder
–files
-Babylon Folder
–files

Both web library have no difficulty seeing these variables but Babylon cant find the variables. How does one set something like this up?

Hi. I think you can’t get global access to variables outside of your program but in browser you have global objects like window so maybe you can declare your global variables in window like global objects?
Something like this Declaring Global Variables in TypeScript — Marius Schulz
I think in this way you can use variables and functions outside your program.

For config.js file you simply can use
fetch(‘http://example.com/movies.json’)
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
//declare your global variable and start program
});

1 Like