Shaghayegh Tavakoli
Shaghayegh Tavakoli

Reputation: 530

Can't access global variable from other files in Node JS

I'm trying to access my config.js file in all modules of my project but I can't even though the config file is defined as global.

Here's my packaging:

enter image description here

in the server.js I have defined:

global.config = require('./app/config');

I can access the properties of config in server.js but when I try to use config in other files such as user.js I get this error:

config is not defined

I don't know how to fix the way I'm defining this global variable.

Upvotes: 1

Views: 1107

Answers (2)

Shaghayegh Tavakoli
Shaghayegh Tavakoli

Reputation: 530

it doesn't make any sense but when I moved the global variable above the variable which contained the router it worked.

global.config = require("./app/config");
const routeHandler = require('./app/routes/index');

Upvotes: 1

GProst
GProst

Reputation: 10227

Most likely config.js file isn't imported into the runtime of the program that you start. Make sure you import config.js at least once into your runtime

Upvotes: 1

Related Questions