How can I use the modules loaded from other Node process from another Node process.
Example I run:
node my_modules
which load MyModule
then I will run another nodejs process:
node grab_modules
which will run GrabModule
GrabModule
will attempt to use the functions inside MyModule
Is this possible? And if this is possible how?
How can I use the modules loaded from other Node process from another Node process.
Example I run:
node my_modules
which load MyModule
then I will run another nodejs process:
node grab_modules
which will run GrabModule
GrabModule
will attempt to use the functions inside MyModule
Is this possible? And if this is possible how?
Share Improve this question edited Dec 29, 2014 at 10:37 Yves M. 31k24 gold badges109 silver badges149 bronze badges asked Jun 4, 2012 at 6:35 Richeve BebedorRicheve Bebedor 1,6584 gold badges27 silver badges42 bronze badges 2- 5 Not sure what you're trying to do here. Please provide a more concrete example of what would be your ideal scenario. – Lior Cohen Commented Jun 4, 2012 at 6:52
- The question could be clearer if you ask me. – Alfred Commented Jun 4, 2012 at 23:00
5 Answers
Reset to default 7What you want is probably dnode:
From the README of dnode:
The server (which hosts the functions to be run):
var dnode = require('dnode');
var server = dnode({
zing : function (n, cb) { cb(n * 100) }
});
server.listen(5050);
The client (which calls functions on the server and gets their results in a callback)
var dnode = require('dnode');
dnode.connect(5050, function (remote) {
remote.zing(66, function (n) {
console.log('n = ' + n);
});
});
It depends on what you are trying to do.
If you simply want to reuse the same module (MyModule) from two separate node processes, that's quite easy. You just need to put require('MyModule') in GrabModule and MyModule is accessible when you run grab_module.
If you want to 'share' MyModule including its global variables among two processes, it is much more plex. You need to define a inter-process protocol between two processes (typically REST over socket), and use that protocol to access the module in one process from another process.
1) To use a module (implementation) not an instance (module loaded somewhere of the process using require) in different process, you only need to require that module wherever you need.
If you run two process, for example, process A that use 'MyModule' and process B that use 'GrabModule', but you only need that 'GrabModule', in process B, can access to the exported properties of 'MyModule' then you only need to use require('path to MyModule').
2) On the other hand, if you need that a process B, can access to a module's state (a module that has been executed, because you use require in somewhere) of a process A, then you need to use a IPC (Inter-process munication) that allows to exchange data between process A and process B, and build or use the same protocol in both, over it.
Depending if your process are in the same machine or in different one, to can use some IPC build in the same OS, as nodejs offer with child fork (http://nodejs/api/child_process.html#child_process_child_process_fork_modulepath_args_options) or use an IPC built in some network channel.
For example, you can use the publish/subscribe messaging system of Redis (http://redis.io/topics/pubsub)
What about this:
my_modules will work as the program with public api (rest api, xml-rpc, ...) and grab_modules will connect to api and call functions from my_modules
If you also require interoperability with other languages and/or high speed, ZeroMQ is also an option. While originally being a plain C library, I have had good experiences with the NodeJS bindings.
There a ZeroMQ binding for almost all popular languages, see http://zeromq/bindings:_start