How can I share module-private data between 2 files in Node? - javascript

I would like to have a module for Node.js that is a directory with several files. I'd like some vars from one file to be accessible from other file, but not from the files external to the module. Is it possible?
So let's suppose the following file structure
` module/
| index.js
| extra.js
` additional.js
In index.js:
var foo = 'some value';
...
// make additional and extra available for the external code
module.exports.additional = require('./additional.js');
module.exports.extra = require('./extra.js');
In extra.js:
// some magic here
var bar = foo; // where foo is foo from index.js
In additional.js:
// some magic here
var qux = foo; // here foo is foo from index.js as well
Additional and Extra are implementing some business logic (independent from each other) but need to share some module-internal service data which should not be exported.
The only solution that I see is to create one more file, service.js and require it from both additional.js and extra.js. Is it correct? Are there any other solutions?

Can you just pass the desired stuff in?
//index.js:
var foo = 'some value';
module.exports.additional = require('./additional.js')(foo);
module.exports.extra = require('./extra.js')(foo);
//extra.js:
module.exports = function(foo){
var extra = {};
// some magic here
var bar = foo; // where foo is foo from index.js
extra.baz = function(req, res, next){};
return extra;
};
//additional.js:
module.exports = function(foo){
var additonal = {};
additional.deadbeef = function(req, res, next){
var qux = foo; // here foo is foo from index.js as well
res.send(200, qux);
};
return additional;
};

Okay, you may be able to do this with the "global" namespace:
//index.js
global.foo = "some value";
and then
//extra.js
var bar = global.foo;

I'd like some vars from one file to be accessible from other file, but not from the files external to the module
Yes, it is possible. You can load that other file into your module and hand it over a privileged function that offers access to specific variables from your module scope, or just hand it over the values themselves:
index.js:
var foo = 'some value';
module.exports.additional = require('./additional.js')(foo);
module.exports.extra = require('./extra.js')(foo);
extra.js:
module.exports = function(foo){
// some magic here
var bar = foo; // foo is the foo from index.js
// instead of assigning the magic to exports, return it
};
additional.js:
module.exports = function(foo){
// some magic here
var qux = foo; // foo is the foo from index.js again
// instead of assigning the magic to exports, return it
};

Related

Passing a parameter to a called module

My app.js contains:
var m1 = require("./m1");
m1.f1(...);
My m1.js contains:
var m1 = module.exports = {};
m1.f1 = function(...) { };
I would like to pass somevariable when loading m1 from app.js:
var m1 = require("./m1")(somevariable);
How can I write m1.js so that the function definition of m1.f1 can access somevariable?
If you want to be able to do this:
var m1 = require("./m1")(somevariable); // it is equivalent to var m = require("./m1"); and then m(someVariable); so m (the exports of the module m1.js) should be a function
then module.exports in m1.js should be a function:
// m1.js:
module.exports = function(theVariable) {
// use variable then return the following object:
return {
f1: function() { /* ... */ }
};
}
So now you can use the module in app.js like this:
// app.js:
var m1 = require("./m1")(someVariable);
m1.f1(/* ... */);
module.exports is the value that will be returned by the call to require when loading the module.
// app.js
require("./m1")(somevar);
// m1.js
module.exports = function(param) {
return "something";
}
The answer above by Ibrahim makes sense if you want to call m1 as a function. What it seems to me, though, is that your use case would be better served by using a JS class.
That way, you can import 'm1' the regular way with require('m1') and then create an instance of M1 with new m1(<whatever you want to pass in>);
Now when you want to call M1, just use m1.f1()
To support classes, your M1 should look something like:
export default class m1 {
constructor(param1, param2) {
this.param1 = param1;
this.param2 = param2;
}
f1() {
do whatever with params here
}
}
The biggest benefit of this is readability. Now when someone (or you) looks at your code, they will see where this input is coming from. Otherwise, it can be hard to track down where the params are coming from. Especially as your code gets more complex.

Unable to use Mongoose Object Methods from non-Mongoose Object

Suppose we have a Mongoose object Foo.js.
var mongoose = require('mongoose');
var Bar = require('/path/to/bar');
var Foo = mongoose.Schema({});
Foo.statics.hello = function() {
console.log('hello from foo');
};
Foo.statics.useBar = function() {
Bar.hello();
};
module.exports = mongoose.model('Foo', Foo);
As well as a regular javascript object Bar.js.
var Foo = require('/path/to/foo');
var Bar = function() {};
Bar.hello = function() {
console.log('hello from bar');
};
Bar.useFoo = function() {
Foo.hello();
};
module.exports = Bar;
If we wanted to call methods in Bar from Foo, everything would be fine. Yet, if we wanted to call methods in Foo from Bar, we would receive an error.
app.use('/test', function(req, res, next) {
var Foo = require('/path/to/foo');
var Bar = require('/path/to/bar');
Foo.hello();
Bar.hello();
Foo.useBar();
Bar.useFoo();
});
The above yields:
hello from foo
hello from bar
hello from bar
TypeError: Foo.hello is not a function
Why does this happen?
Additionally, how do I create an object Bar that can call methods from Foo, but at the same time is not meant to be - and cannot be - persisted into mongodb?
The problem you are experiencing is circular/cyclic dependencies in node.js. It gives you an empty object.
If you change Bar.js like this:
var Bar = function() {};
module.exports = Bar;
var Foo = require('/path/to/foo');
Bar.hello = function() {
console.log('hello from bar');
};
Bar.useFoo = function() {
Foo.hello();
};
and then swap the order in app.use to
var Bar = require('/path/to/bar');
var Foo = require('/path/to/foo');
it works for me.
Look at this answer for more information: How to deal with cyclic dependencies in Node.js

Using an exposed object as value or helper in Express.js with jade templating?

I'm exposing (for testing purposes) an object and a named function:
var express = require('express')
, expose = require('express-expose')
, app = express();
// ...
// Expose with 'utils' namespace, default 'javascript' buffer
app.expose({ foo: 'bar' }, 'utils');
// Expose named function with 'funcs' buffer
app.expose(function foo() { return 'bar'; }, 'funcs');
JavaScript variables are created just fine:
doctype 5
html(lang="en")
head
meta(charset='utf-8')
title
// Buffer 'javascript'
script!= javascript
// Buffer 'funcs'
script!= funcs
body
Output:
<!-- Buffer 'javascript'-->
<script>
window.utils = window.utils || {};
utils["foo"] = "bar";
</script>
<!-- Buffer 'funcs'-->
<script>
function foo() { return 'bar'; }
</script>
But I'd like to access both object { foo: 'bar' } and function foo() { return 'bar'; } from inside Jade. According to express.expose both are available in Jade... but where?
You can add locals to your jade templates application wide by adding them to app.locals.
app.locals.foo = function foo() { return 'bar'; }

Call function of app.js from within module in node.js?

Let's say I have the following app.js (obviously very simplified):
var express = require('express'),
app = express.createServer();
// include routes
require('./lib/routes')(app);
// some random function
var foo = function() {
return 'bar';
};
// another random function
var foo2 = function() {
return 'bar2';
};
And then I have the routes module:
module.exports = function(app){
app.get('/some/route', function(req, res){
var fooBar = foo(),
fooBar2 = foo2();
res.end(fooBar + fooBar2);
});
};
This obviously doesn't work since foo and foo2 weren't defined within the module. Is there a way to make this work, or at least a different pattern to better accomplish what this?
Well you can just put these two functions in an object and pass them on the initialization of the routes.js .
var express = require('express'),
app = express.createServer();
// some random function
var foo = function() {
return 'bar';
};
// another random function
var foo2 = function() {
return 'bar2';
};
var fns = {foo : foo, foo2: foo2}
// include routes
require('./lib/routes')(app, fns);
in routes:
module.exports = function(app, fns){
app.get('/some/route', function(req, res){
var fooBar = fns.foo(),
fooBar2 = fns.foo2();
res.end(fooBar + fooBar2);
});
};
This is how would I do it. You can also include them in the app object. Beside passing them in init functions, you can also export those two functions and require them in routes.js.
var express = require('express'),
app = express.createServer();
// some random function
var foo = function() {
return 'bar';
};
// another random function
var foo2 = function() {
return 'bar2';
};
module.exports = {foo : foo, foo2: foo2}
// include routes
require('./lib/routes')(app, fns);
in routes:
module.exports = function(app){
var fns = require('../app.js');
app.get('/some/route', function(req, res){
var fooBar = fns.foo(),
fooBar2 = fns.foo2();
res.end(fooBar + fooBar2);
});
};
But I don't like the idea of it, since it makes circular dependencies. Don't have any good feelings about them.

Share variables between files in Node.js?

Here are 2 files:
// main.js
require('./module');
console.log(name); // prints "foobar"
// module.js
name = "foobar";
When I don't have "var" it works. But when I have:
// module.js
var name = "foobar";
name will be undefined in main.js.
I have heard that global variables are bad and you better use "var" before the references. But is this a case where global variables are good?
Global variables are almost never a good thing (maybe an exception or two out there...). In this case, it looks like you really just want to export your "name" variable. E.g.,
// module.js
var name = "foobar";
// export it
exports.name = name;
Then, in main.js...
//main.js
// get a reference to your required module
var myModule = require('./module');
// name is a member of myModule due to the export above
var name = myModule.name;
I'm unable to find an scenario where a global var is the best option, of course you can have one, but take a look at these examples and you may find a better way to accomplish the same:
Scenario 1: Put the stuff in config files
You need some value that it's the same across the application, but it changes depending on the environment (production, dev or test), the mailer type as example, you'd need:
// File: config/environments/production.json
{
"mailerType": "SMTP",
"mailerConfig": {
"service": "Gmail",
....
}
and
// File: config/environments/test.json
{
"mailerType": "Stub",
"mailerConfig": {
"error": false
}
}
(make a similar config for dev too)
To decide which config will be loaded make a main config file (this will be used all over the application)
// File: config/config.js
var _ = require('underscore');
module.exports = _.extend(
require(__dirname + '/../config/environments/' + process.env.NODE_ENV + '.json') || {});
And now you can get the data like this:
// File: server.js
...
var config = require('./config/config');
...
mailer.setTransport(nodemailer.createTransport(config.mailerType, config.mailerConfig));
Scenario 2: Use a constants file
// File: constants.js
module.exports = {
appName: 'My neat app',
currentAPIVersion: 3
};
And use it this way
// File: config/routes.js
var constants = require('../constants');
module.exports = function(app, passport, auth) {
var apiroot = '/api/v' + constants.currentAPIVersion;
...
app.post(apiroot + '/users', users.create);
...
Scenario 3: Use a helper function to get/set the data
Not a big fan of this one, but at least you can track the use of the 'name' (citing the OP's example) and put validations in place.
// File: helpers/nameHelper.js
var _name = 'I shall not be null'
exports.getName = function() {
return _name;
};
exports.setName = function(name) {
//validate the name...
_name = name;
};
And use it
// File: controllers/users.js
var nameHelper = require('../helpers/nameHelper.js');
exports.create = function(req, res, next) {
var user = new User();
user.name = req.body.name || nameHelper.getName();
...
There could be a use case when there is no other solution than having a global var, but usually you can share the data in your app using one of these scenarios, if you are starting to use node.js (as I was sometime ago) try to organize the way you handle the data over there because it can get messy really quick.
If we need to share multiple variables use the below format
//module.js
let name='foobar';
let city='xyz';
let company='companyName';
module.exports={
name,
city,
company
}
Usage
// main.js
require('./modules');
console.log(name); // print 'foobar'
Save any variable that want to be shared as one object. Then pass it to loaded module so it could access the variable through object reference..
// main.js
var myModule = require('./module.js');
var shares = {value:123};
// Initialize module and pass the shareable object
myModule.init(shares);
// The value was changed from init2 on the other file
console.log(shares.value); // 789
On the other file..
// module.js
var shared = null;
function init2(){
console.log(shared.value); // 123
shared.value = 789;
}
module.exports = {
init:function(obj){
// Save the shared object on current module
shared = obj;
// Call something outside
init2();
}
}
a variable declared with or without the var keyword got attached to the global object. This is the basis for creating global variables in Node by declaring variables without the var keyword. While variables declared with the var keyword remain local to a module.
see this article for further understanding - https://www.hacksparrow.com/global-variables-in-node-js.html
Not a new approach but a bit optimized. Create a file with global variables and share them by export and require. In this example, Getter and Setter are more dynamic and global variables can be readonly. To define more globals, just add them to globals object.
global.js
const globals = {
myGlobal: {
value: 'can be anytype: String, Array, Object, ...'
},
aReadonlyGlobal: {
value: 'this value is readonly',
protected: true
},
dbConnection: {
value: 'mongoClient.db("database")'
},
myHelperFunction: {
value: function() { console.log('do help') }
},
}
exports.get = function(global) {
// return variable or false if not exists
return globals[global] && globals[global].value ? globals[global].value : false;
};
exports.set = function(global, value) {
// exists and is protected: return false
if (globals[global] && globals[global].protected && globals[global].protected === true)
return false;
// set global and return true
globals[global] = { value: value };
return true;
};
examples to get and set in any-other-file.js
const globals = require('./globals');
console.log(globals.get('myGlobal'));
// output: can be anytype: String, Array, Object, ...
globals.get('myHelperFunction')();
// output: do help
let myHelperFunction = globals.get('myHelperFunction');
myHelperFunction();
// output: do help
console.log(globals.set('myGlobal', 'my new value'));
// output: true
console.log(globals.get('myGlobal'));
// output: my new value
console.log(globals.set('aReadonlyGlobal', 'this shall not work'));
// output: false
console.log(globals.get('aReadonlyGlobal'));
// output: this value is readonly
console.log(globals.get('notExistingGlobal'));
// output: false
With a different opinion, I think the global variables might be the best choice if you are going to publish your code to npm, cuz you cannot be sure that all packages are using the same release of your code. So if you use a file for exporting a singleton object, it will cause issues here.
You can choose global, require.main or any other objects which are shared across files.
Otherwise, install your package as an optional dependency package can avoid this problem.
Please tell me if there are some better solutions.
If the target is the browser (by bundling Node code via Parcel.js or similar), you can simply set properties on the window object, and they become global variables:
window.variableToMakeGlobal = value;
Then you can access this variable from all modules (and more generally, from any Javascript context).

Categories

Resources