1

I am making a library in NodeJS and it requires some configuration variables
I want to be able to define those variables in the main js file, and be able to use them in the core.js file like so

var config1 = "stuff"; 
var config2 = "more stuff;

var db = require('./core.js'); //this file uses config1 and config2

Is there any way to do this WITHOUT using exports.config1 and including the file in core.js?
I'm sorry if this is obvious but I couldn't find anything on google.

EDIT
I used a class with constructor

var config1 = "stuff"; 
var config2 = "more stuff;

var tt = require('./core.js');
const db = new tt(config1, config2);
Amateurz
  • 25
  • 5

1 Answers1

0

One way to do this is define them as global variables.

main.js

global.config1 = "stuff"; 
global.config2 = "more stuff;

Then you can access them in the main.js.

core.js

console.log(global.config1)

Also please note that global is deprecated. You'll see the following error.

(node:59502) DeprecationWarning: 'GLOBAL' is deprecated, use 'global'

Another thing to note is that you should be cautious with putting variables in the global scope.

Ishan Thilina Somasiri
  • 1,179
  • 1
  • 12
  • 24