Node.js: Require locals - node.js

What I want
Is it possible to pass locals to a required module?
For example:
// in main.js
var words = { a: 'hello', b:'world'};
require('module.js', words);
// in module.js
console.log(words.a + ' ' + words.b) // --> Hello World
I'm asking this because in PHP when you require or include, the file which includes another files inherits it's variables, which is very useful in some cases, and I would be happy if this could be done in node.js too.
What I have tried and didn't worked
var words = { a: 'hello', b:'world'};
require('module.js', words);
var words = { a: 'hello', b:'world'};
require('module.js');
Both of these gives ReferenceError: words is not defined when words is called in module.js
So is it possible at all without global variables?

What you want to do is export it with an argument so you can pass it the variable.
module.js
module.exports = function(words){
console.log(words.a + ' ' + words.b);
};
main.js
var words = { a: 'hello', b:'world'};
// Pass the words object to module
require('module')(words);
You can also chop off the .js in the require :)

The question is: What do you want to achieve?
If you want to export just a static function you can use the answer from tehlulz. If you want to store an object inside the exports property and benefit from the require-caching node.js provides a (dirty) approach would be to you globals. I guess this is what you have tried.
Using JavaScript in a Web-Browser context you can use the window object to store global values. Node provides only one object that is global for all modules: the process object:
main.js
process.mysettings = { a : 5, b : 6};
var mod = require(mymod);
mymod.js
module.exports = { a : process.mysettings.a, b : process.mysettings.b, c : 7};
Alternatively if you are not interested in the exports caching you could do something like that:
main.js
var obj = require(mymod)(5,6);
mymod.js
module.exports = function(a,b){
return { a : a, b : b, c : 7, d : function(){return "whatever";}};
};

Related

How to access variables in NodeJS

I would like to understand best practice for accessing variables that are defined in an external module that would then be required in several other files.
Say we have the two following files, I would like to access the value of the h variable in consume.js
//central.js
module.exports = {
common: function() {
var h = "hello";
var b = "enabled"
}
};
and
//consume.js
var g = require('./central');
//get the value of variable h from central.js
Taking this a step further, if I have the following consume.js, fetch.js and get.js files that all imported central.js, and required a common set of variables from central.js, how does one go about defining these common variables inside central.js so that the dependent files can consume them?
Thanks!
Export:
// central.js
module.exports = {
h: "hello",
b: "enabled"
}
Import:
// consume.js
const { h, b } = require('./central')
Or alternatively:
// get.js
const central = require('./central')
// central.h
// central.b
Hope this helps!

is this use of eval in a node server a bad idea?

I want to create an object dynamically in node. To do that i used a code like this.
Is this use of eval in a node server a bad idea?
var a1 = require(./a1.js),
a2 = require(./a2.js),
...
aN = require(./aN.js);
function createObj(pObjName, pObjValue){
var tmp = new eval(pObjName)(pObjValue);
//where pObjName is a1 or a1 or .... or aN
}
From what you're showing, there's no need to use eval:
const Classes = {
a1 : require('./a1'),
a2 : require('./a2'),
...
};
function createObj(pObjName, pObjValue){
var tmp = new Classes[pObjName](pObjValue);
...
}
Seems like you want to create objects with a set of properties?
You might want to look at the Object.create()-method
If you want to save yourself the time of declaring all of those dependencies, you could even write a function without using eval that handles the case even more efficiently:
function createObj(pObjName, pObjValue) {
var tmp = new (require('./' + pObjName))(pObjValue);
// ...
}
Please note that this is only safe if createObj() is guaranteed to be invoked with a pObjName that is what you expect, otherwise you'll need to validate it first, possibly something like this:
function createObj(pObjName, pObjValue) {
if (!/^a\d$/.test(pObjName)) {
throw new TypeError('invalid name')
}
var tmp = new (require('./' + pObjName))(pObjValue);
// ...
}

How to use dynamic variable name in nodejs

I need a dynamic variable in nodejs .
I use the allocine-api, and she return, a different object when I use it . For example :
allocine.api(type, {code: code}, function(error, result) {
if(error){
console.log('Error : '+ error);
return;
}
socket.emit("allocine_"+type ,result);
});
if type is "movie", result contain a movie object, but if type is "tvseries", result contain a tvseries object. So I need to take the variable "originalTitle" in "tvseries" or "movie" object, so I need to make this :
result.<type>.originalTitle
But, how to use the contain of "type" for this ?
I have try with the javascript method, and the use of "window['type']", but it's don't work in nodeJs .
as javascript objects elements can be accessed as an associative array ( cf mozilla js doc )
using myobject.myproperties is strictly equal to use myobject["myproperties"]
so if a var hold the properties name to read var myvar = "myproperties"; you could also use myobject[myvar]
so, concretely :
var o = {
tvseries : {
originalTitle : "hello world"
}
}, type = "tvseries";
console.log( o[type].originalTitle );
jsfiddle
also, if the result object get only one sub object, named by the type, you can get type name directly from it
var type = Object.keys( myobject )[0];
jsfiddle
or more simply :
var theTitle = myobject[ Object.keys( myobject )[0] ].originalTitle;
jsfiddle
var results = {};
results.a = {originalTitle: "One"};
var results2 = {b:{originalTitle: "Two"}};
console.log(results['a'].originalTitle); //now one
console.log(results2['b'].originalTitle); //now Two
With two ways of creating the object.
Try the fiddle:
http://jsfiddle.net/rmoskal/6rf0juq6/

Sharing & modifying a variable between multiple files node.js

main.js
var count = 1;
// psuedocode
// if (words typed begins with #add)
require('./add.js');
// if (words typed begins with #remove)
require('./remove.js');
// if (words typed begins with #total)
require('./total.js');
module.exports.count = count;
total.js
var count = require('./main.js').count;
console.log(count);
add.js
var count = require('./main.js').count;
count += 10;
console.log(count);
remove.js
var count = require('./main.js').count;
count -= 10;
console.log(count);
console.log
1
11
-9
Background:
I have an application (irc bot), and I want to add a feature that peeps can do #add 1 or #remove 1. I have a main.js that then requires different files depending on the triggers that are said. So add would trigger the add.js file, and that would then require('main.js') and add 10 (10 for simplification, it'll actually parse the number and use that number) to it. The problem I'm having is when someone goes about and does #remove. It require('main.js') and subtracts 10 from 1 resulting in -9. And doing #total would output 1.
I've done a fairly good search for module.exports and I haven't come across an example like the one i listed above. The docs don't include any examples close to what I'm wanting to do; and these questions 1, 2 I understand--but aren't of any usefulness to me--as I understand what's being said there.
Question:
I'd like to have both #add and #remove manipulate the same variable ( count ), and for #total to return the total of count with the #add and #removes taken into account. Am I using module.exports incorrectly; or is there a common way that variables are shared, with one file being able to modify the contents of the module.exports and returning the results to the main.js file?
Your problem is that when you do var count = require('./main.js').count;, you get a copy of that number, not a reference. Changing count does not change the "source".
However, you should have the files export functions. Requiring a file will only run it the first time, but after that it's cached and does not re-run. see docs
Suggestion #1:
// main.js
var count = 1;
var add = require('./add.js');
count = add(count);
// add.js
module.exports = function add(count) {
return count+10;
}
#2:
var count = 1;
var add = function() {
count += 10;
}
add();
#3: Personally i would create a counter module (this is a single instance, but you can easily make it a "class"):
// main.js
var counter = require('./counter.js');
counter.add();
console.log(counter.count);
// counter.js
var Counter = module.exports = {
count: 1,
add: function() {
Counter.count += 10;
},
remove: function() {
Counter.count += 10;
}
}
Not sure if this new or not but you can indeed share variables between files as such:
main.js
exports.main = {
facebook: null
};
counter.js
var jamie = require('./main');
console.info(jamie); //{facebook: null}
jamie.main.facebook = false;
console.info(jamie); //{facebook: false}
anothercheck.js
var jamie = require('./main');
console.info(jamie); //{facebook: null} //values aren't updated when importing from the same file.
jamie.main.facebook = true;
console.info(jamie); //{facebook: true}
Now you can share between files.
I know I'm a little bit late to answer this questions, just 7yrs!
You can simply use a global variable:
global.myVar = 'my-val';
console.log(myVar); // returns 'my-val'
// from here on it's accessable to all modules by just the variable name
using-global-variables-in-node-js
I have same problem like you,.. Sometimes I'd like to sharing variables between multiple files because I love modular style eg. separating controller, function, models in different folders/files on my node.js script so I can easy manage the code.
I don't know if this is the best solution but I hope will suit your needs.
models/data.js
// exports empty array
module.exports = [];
controllers/somecontroller.js
var myVar = require('../models/data');
myVar.push({name: 'Alex', age: 20});
console.log(myVar);
// [{ name: 'Alex', age: 20 }]
controllers/anotherController.js
var myVar = require('../models/data');
console.log(myVar);
// This array has value set from somecontroller.js before...
// [{ name: 'Alex', age: 20 }]
// Put new value to array
myVar.push({name: 'John', age: 17});
console.log(myVar);
// Value will be added to an array
// [{ name: 'Alex', age: 20 }, { name: 'John', age: 17}]
There is no way you can share a reference between different files. You shouldn't be.
I have a main.js that then requires different files depending on the triggers that are said
I don't think that's a good idea. All require statements you'll ever need must be at the top of the file.
I also see that You're requiring main.js in total.js and total.js in main.js. The require() function imports the module.exports of the file and assigns it to the namespace you provide. Your code shouldn't be split into files this way. You extract code into separate files only when they're modules by themselves. And if you do, you wouldn't be importing 2 files on each other.
It is also good to note that in javascript, when you assign something to a namespace, It gets copied (cloned) if it's a primitive. If it's an object, both namespaces then refer to the same object
var num = 5;
var prim = num;
prim++; // prim is 6, but num is still 5.
var num = {five:5};
var ob = num;
ob.five = 6;
console.log(num.five) //also 6.
A little hack that works but isn't recommended is using the process variable. You can apply different properties to it and essentially use them like you would the window object in browser-based JS. This little hack will provide a reference to the variable. It can be changed and manipulated and the change will carry over to all files that are required.
But do note that it is not recommended as overriding the process variable could have some unexpected effects and is subject to loss of information should another process interfere.
file1.js:
const s1 = require('./file2');
process.num = 2;
console.log("file1", process.num);
s1.changeNum();
console.log("file1", process.num);
file2.js:
module.exports.changeNum = () => {
process.num = 3;
console.log("file2", process.num);
};
output:
file1 2
file2 3
file1 3
alternatively, to all other answers
getters & setters
var _variableThing = 1223
module.exports = {
get variableThing(){
return _variableThing
},
set variableThing(val){
_variableThing = val
}
}
won't work with direct imports though

var v1 = new class(params) , object = {}

Hi I don't know what exactly do this kind of sentences:
var v1 = new class(params) , object = {}
The real example was: (From https://github.com/visionmedia/parted Usage paragraph)
var parser = new multipart(type, options) , parts = {};
I understand that parser will be a new multipart object, but parts?! what exactly do? Create empty object? where I have to push some data?
Thank's in advice!
var declarations can take multiple variables. Example:
var a = 1, b = 2;
That declares two variables, a and b, and assigns them the values 1 and 2, respectively.
In other words, in your example, parts is a new variable that is assigned an "empty" object.
{} in javascript creates a new empty object using JavaScript's literal object notation.
Other examples of creating objects using the literal object notation:
obj1 = { foo: 2 } // Now obj1.foo is 2
obj2 = { foo: 3, bar: "hello" } // Now obj2.foo is 3 and obj2.bar is "hello"

Resources