NodeJS: Static imports possible? - node.js

Question in short:
Is there a way to statically import functions of another JS file in NodeJS? (As the static-import of Java?)
Example of what I'd like to do:
I have a file m1.js which contains functions:
function add(x,y) { return x + y }
exports.add = add
Then I have a file app.js which imports m1.js:
m1 = require('./m1')
var result = m1.add(3,4)
Now, what I'd like to do is to import the functions of m1.js such that i can call them, without having to prefix the calls with m1.*:
m1 = require('./m1')
var result = add(3,4) // instead of m1.add(3,4)
What I've tried so far:
I've tried the following, in the file m1.js:
function add(x,y) { return x + y }
exports.static = function(scope) { scope.add = add }
and tried to import m1.js in app.js as follows but it wasn't able to find add(x,y):
require('./m1').static(this)
var result = add(3,4)

You were close with your attempt. The one small change you have to make is replace this with global when static is called:
require('./m1').static(global)
var result = add(3,4)
From the documentation:
global
{Object} The global namespace object.
In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope var something will define a global variable. In Node this is different. The top-level scope is not the global scope; var something inside a Node module will be local to that module.

Related

Extend module with global configuration without breaking current usages

I want to extend a current Node.js module with some global config setting being configured once without breaking current usages of this module.
This is the signature of the module:
const myFunction = function(someOptions) { ... };
module.exports = myFunction;
Usage is
const myFunction = require('myfunction');
const result = myFunction(options);
Now I want to set some options on application startup to be used by the module myfunction whenever being required without breaking current usages of the module myfunction.
If possible, I want to avoid using Node.js global.
Functions in JavaScript are just objects, so you can give them properties. This is a little hacky, but it could certainly work for you:
// greeter.js
function sayHello() {
const message = sayHello.message || "Hi";
console.log(message);
}
module.exports = sayHello;
You can now set the config of this function globally as follows:
const sayHello = require("./greeter.js");
sayHello.message = "S'up dawg";
Any subsequent calls to sayHello() after the code above is executed will use the overridden message. This works because calls to require() are cached, so each time you require(./greeter.js); you're getting back exactly the same function object.

How to avoid using global functions in NodeJS?

I have a global function in my main JS file and i need to call it several times from a module. I heard it is bad because it can create a function name collision between a local (from module) and a global function name (from main js). The global function can be called 100 times from the require module. Is there an alternative way to achieve the following without using global functions?
main.js with global function
const myMod = require('./module.js");
global.myGlobalFunction = function(param){
console.log("do something with",param);
}
module.js with the calls of the global function:
module.exports = function(){
myGlobalFunction("class");
myGlobalFunction("this");
myGlobalFunction("bubu");
// other jobs to do
}
The neater way to do it is to use modules like this:
module.js
class myGlobalUtil {
myGlobalFunction(param){
console.log("do something with",param)
}
get GetFromParam(myParam){
return this.myGlobalFunction(myParam);
}
}
module.exports = myGlobalUtil;

Best way to invoke code inside a module in nodejs

From a performance perspective, is there any difference between invoking code by wrapping it in a function and then exporting it:
function doSomething () {
// doing something here
}
module.exports = doSomething();
And just requiring it without any exports? like this:
myModule.js
// Code doing something
file that requires the module:
var doSomething = require('./myModule');
And if the purpose of the code inside the module is to run just once, do I need to store it in a variable?
If you don't need the return value of that function, then you don't have to store it in a variable.
The difference with:
function doSomething () {
// doing something here
}
module.exports = doSomething();
and using:
var x = require('module');
var y = require('module');
vs.
function doSomething () {
// doing something here
}
module.exports = doSomething;
and using:
var x = require('module')();
var y = require('module')();
is that in the first case, the function will be run only once, while in the second case the function will be run twice.
The difference is that if you just include it without module.exports, then the code will execute immediately but be private to the module. You can only access the data if you export it somehow, with module.exports. It can be either a function or a Javascript Object. Essentially, you can view everything within the module as being completely hidden from everything else in your application.
The only shortcut that I know of is for JSON files. If you look here: Module.exports vs plain json for config files, you can see that you can require('file.json') and it will replace the contents of the json file with a Javascript object that you can then use in your application.

How to Use one module feature within another module in nodejs require()

I want to use one module feature within another module
file main.js
var _ = require("./underscore.js");
var foo = require("./bar.js");
foo.publish(...);
file bar.js
(function(e) {
var array = [...];
e.publish = function(t, args) {
_.each(array, function(...) {...});
});
})(exports);
I've tried a couple of variations, but I am not sure of the best way around this error:
ReferenceError: _ is not defined
Yes, you should use in every module which needs that variable, for the case of you example.
var _ = require("./underscore.js");
But if you need to transfer one object instance between several modules you can use process object to make it global. But that is BAD practice.
process._ = require("./underscore.js");
Good way to pass object instances between the modules is to pass them as function arguments, but you need to change you bar.js to return a factory function, not an object itself.
module.exports = function(_) {
var e = {};
var array = [...];
e.publish = function(t, args) {
_.each(array, function(...) {...});
});
return e;
}
In main.js:
var _ = require("./underscore.js");
var foo = require("./bar.js")(_);
foo.publish(...);
I hope you got the point.
The variable to which you assign the result is local to the module main.js, so you can't access it in bar.js. Instead, you should require the underscore module in bar.js as well.
You could also skip the var when declaring _ and make it a global variable, but that brings all the problems of global variables, like:
the bar.js module is not explicit about its dependencies, so it's harder to understand that it expects underscore to be required globally;
it requires a specific initialization order - if you later move the _ = require(underscore), you'll get undefined errors again, and it might be hard to understand why;
every other module that requires bar.js needs to also require underscore.

NodeJS Functions/Module Include Scope

I'm developing an application with node.js and socket.io. I have built a small-scale project with some variables and functions inside a connection-specific block. These functions have access to the variables declared within that block without the need to pass the values in specifically.
This works fine, and is acceptable for a project of this size. However, as I was trying to clean the code up a bit, I looked into factoring those functions out into their own file and found modules declared by using exports.functionname as described here: http://nodejs.org/docs/v0.3.2/api/modules.html
However, these functions do not have access to the variables within the same block as they normally do when being require()'d in instead of actually being declared in the file.
Is there some way to make functions in an external file behave as if they were declared locally in nodejs?
There isn't without hacking the module system, which is exactly what I did. https://github.com/Benvie/Node.js-Ultra-REPL/blob/master/lib/ScopedModule.js
I can't say I recommend it for production. Basically the issue is more with JavaScript itself. Node wraps modules in a function so they have their own private scope. The only way to share in that scope is to be executed inside that function which wouldn't really work for a module (modular...) system. The only other scope is global which also isn't desirable.
The trick I used to get around it is in changing that wrapper function to have dynamic properties based on an externally defined set of module imports so that from inside the module wrapper all those parameters look like they're magically defined but aren't global.
Node's module wrapper looks like this:
(function (exports, module, require, __filename, __dirname){
/**module**/
})
Where mine simply adds more parameters and ensures they're resolved before executing the module wrapper.
I have it read a file named ".deps.json" in a given folder before loading a module.
An example one would be like this
[{
"providers": [ "./utility/",
"./settings/" ],
"receivers": [ "*" ]
}]
So it will load the modules in those subfolders and then expose each one as a parameter in the wrapper, named based on the filename.
The usual and clean way would be to define a function in your module, that takes the required variables as parameters:
// extracted_file.js
exports.handler = function(param1, param2) {
return param1 + param2;
}
// caller file
var extractedHandler = require('./extracted_file').handle;
var localVar1 = 1300,
localVar2 = 37;
console.log(extractedHandler(localVar1, localVar2));
You can change function scope using toString() and the dreaded eval.
If your functions were in say lib.js as follows:-
var lib = {}
lib.foo = function() { console.log(var1) }
lib.bar = function() { console.log(var2) }
module.exports = lib
In main.js you could have this:-
var var1 = 1
var var2 = 2
var ob1 = require('./lib')
ob1.foo()
ob1.bar()
When you run it the ob1.foo() line gives a ReferenceError, var1 is not defined. This is because the scope of foo() comes from lib.js and not your main js file, and var1 is invisible to it. ob1.foo is a reference to a function with lib.js scope. You need to make a new reference with main.js scope.
To do this convert each function to string and eval it. That will create a new function with the same code but with main.js scope. Here's one way using a setMainScope method which loops through all the functions changing their scope to main.js.
var var1 = 1
var var2 = 2
var ob1 = {}
ob1.setMainScope = function(ob2) {
for(var prop in ob2) {
if( !ob2.hasOwnProperty(prop) ) {continue}
if( typeof ob2[prop] !== 'function' ) {continue}
this[prop] = eval( "(" + ob2[prop].toString() + ")" )
}
}
ob1.setMainScope( require('./lib') )
ob1.foo() // 1
ob1.bar() // 2
Now it all works nicely except eval was used. The scope of foo() and bar() is now main.js and so they 'see' var1 and var2.

Resources