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

Shaghayegh Tavakoli

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.

Shaghayegh Tavakoli

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');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

can't access to public files NODE JS

Can't use a global variable from JS with AJAX call in other script element on same html page

NODE.JS Functions declared in module can access variable but same function imported from other module cannot

Why can't I access a global variable from AsyncTask?

Node.js Can't access variable in another javascript file

Can't access variable in custom Node.js module

LLVM IR can't access global variable

Why can't I access this global variable?

jquery global variable can not access from outside

How to make a global variable or be able to access it in other files

I can't access data from api in node js

Can't pass a variable from Node.js to Jade

R Shiny: Why can't functions from source files access global variables when running as an app?

How can I access and modify a global variable across multiple files?

Can't access variable (array) from another function (defined in global scope)

How can i modify a global variable from within a callback function . in node.js

Assigning local variable from global variable node js

why can't other person access node.js server that i made?

can't access to a global variable in event handler method (javascript)

Why can't i access the value in the global variable

Java Predicate implementation can't access global final variable

Can't Access Value of Input Using Global or Local Variable

Why can't I access a declared variable in the global scope

|SOLVED| Discord.js Node.js | Can't access a JSON file from an "exported" JS file

Can't access request data in Node JS

static variable's address can access by other files or not

Working with global window variable in mocha js from node

Can't access files from within makefile

Can't append global NumPy array from other module