Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's considered a good way to share "global" variables inside a node module?

How can I easily refer to other directories in my module without explicitly relative paths (../../../lib..)?

I'm writing a node module and there are some global things I want to reuse in my module.
Most basically - I want to set the root path of the module as a 'global' so I can easily call other sources without using a lot of relative paths ../../ and things like that. It can cause messy code and it's easy to mistake or miss it if the project structure changes.

So I've seen a lot of options in that post and some other libraries for dealing with those kind of things (such as modules that give the root path - app-module-path, rootpath, rfr etc.) but they all refer to a base project/application and not to a module that's being used by others.

Setting a global is a bad idea, and I understood that an environment variable is also not such a good idea.

Is there a good practice for that thing? Maybe there's something I haven't found or heard of.

Here's an example of what I'm trying to avoid and what I'm looking for:

// avoid things like that:

// ./lib/something/forthat/doit.js
var config = require('../../../config/project/type1/config.js');

// ./config/project/type1/config.js
module.exports = {
  msg: 'hi'
};


// find somethings like that:
// when the root path/require can be found in every location of the module
// and is relative to my app and not the app using my module.

// ./lib/something/forthat/doit.js
var config = require(rootPath + 'config/project/type1/config.js');
// OR
var config = rootRequire('config/project/type1/config.js');
// OR anything else

// ./config/project/type1/config.js
module.exports = {
  msg: 'hi'
};
like image 788
arieljannai Avatar asked Feb 15 '16 09:02

arieljannai


People also ask

How can you share global variables across modules?

We can create a config file & store the entire global variable to be shared across modules or script in it. By simply importing config, the entire global variable defined it will be available for use in other modules.

How do you access global variables in nodeJS?

To set up a global variable, we need to create it on the global object. The global object is what gives us the scope of the entire project, rather than just the file (module) the variable was created in. In the code block below, we create a global variable called globalString and we give it a value.

What is the best way to declare and access a global variable?

The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.

How global variables are accessed from module?

A global variable is a variable which is accessible in multiple scopes. In Python, it is better to use a single module to hold all the global variables you want to use and whenever you want to use them, just import this module, and then you can modify that and it will be visible in other modules that do the same.


1 Answers

For getting the current directory path you can use the global variable __dirname any where in your nodejs project. Example : console.log(__dirname) write this into any file in your project and console would print the current directory path as string. Or, You can use express-session module like;

var express = require('express');
var session = require('express-session');
var app = express();
app.use(session({myVar: 'abc'}));
app.get('/',function(req,res){
var sess = req.session;
console.log(sess);
});
like image 164
Sanjeev Kumar Avatar answered Sep 23 '22 14:09

Sanjeev Kumar