Nodejs - initialize variables and they are available throughout the project

Asked

Viewed 18 times

0

I’m having the following difficulty:

I have the following design structure:

inserir a descrição da imagem aqui

my server.js is the start of the project. However, within the services, models, etc I have some libs, as an example log which according to Environment it shows on the console or goes to a log file.

So here are two items to question and ask for your help: 1 - I need to put the require in the lib of log in all files? I know there is global. , but in the vscode, it does not show the scope. It only shows with const, var ou let.

See the example below with global. and with const

inserir a descrição da imagem aqui

with global shows server scope

-

inserir a descrição da imagem aqui

and with overall type is any

2 - this function I have in the log, which is the setenv(env) I also need to set in all files?

  • 1

    It’s almost never a good idea to use global variables for the application. It is worth much more you create a module and use it as such: import it where you need it.

  • then your suggestion is to have a module type env.js that will initialize all these conditions?

  • something else...I know it runs a little bit from the original question, but what is the best way to deal with relative paths?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.