[nodejs] require remote modules

735 views
Skip to first unread message

Joson Mandaro

unread,
Aug 31, 2010, 4:34:30 AM8/31/10
to nod...@googlegroups.com
Hi,
I have some modules on the other pc (url = "http://192.168.1.11:8080/a.js"). How to load this file remotely?
There is a function "loadModule" in node.js.  when I call it loadModule(url, self, callback), it threw out an error: loadModule is undefined.
And I found require() function can't get the remote file.
Best regards,
Joson

Bradley Meck

unread,
Aug 31, 2010, 8:49:55 AM8/31/10
to nodejs
Short answer: you dont require a module over networks without work

Long answer:
spawn up an http.createClient, requests the script, buffer the data
from the response, fire off this: "var module = new Module(url);
module._compile(contents,filename);module.loaded = true"
the arguments to Module will be the key the module is cached on so be
wary. The "request" module for node on github makes this fairly easy.

A big reason this isnt supported is the blocking nature of require and
the non-blocking nature of all network io in node. (dont want to wait
10^6~ as long for a net file that blocks compared to a fs one)

Isaac Schlueter

unread,
Aug 31, 2010, 1:43:46 PM8/31/10
to nod...@googlegroups.com
require.async( "http://192.168.1.11:8080/a.js", function (er, a) {
if (er) return console.error("Failed loading a.js")
doSomething(a)
})

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>

Kris Zyp

unread,
Aug 31, 2010, 11:33:06 PM8/31/10
to nodejs
Doing require("http://192.168.1.11:8080/a.js") is supported with
nodules [1]. Static analysis is used to fetch the module
asynchronously, so the require can return synchronously.
[1] http://github.com/kriszyp/nodules
Kris

Joson Mandaro

unread,
Sep 2, 2010, 12:09:41 AM9/2/10
to nod...@googlegroups.com
thank all of you. I've read the nodules. It's a very useful tool, thanks.

2010/9/1 Kris Zyp <kri...@gmail.com>

--
Reply all
Reply to author
Forward
0 new messages