11

I've been working on an online socket server using NodeJS and javascript, and I've been creating "playrooms" in my code using require:

new_game_obj = require('./forza4.js');

Now.. this works find when I test my code on my local machine, but for the production server I've been presented with a problem. It would seem that for some technical reason, the process that runs my code is on a different machine then the one I have access to (for file uploading, and such), so I was asked by the guys on the server farm to change my code so that I will load the code I have in "forza4.js" from a global position, and not local, like I do at the moment. So I changed the code to this:

new_game_obj = require('http://www.xxxxx.com/blabla/forza4.js');

(Of course I tested to see if the file is there, just to be sure, it shows in the browser when I point to that actual address) But I get an error from my code (again, at this point, I'm running this locally on my machine), which says:

Exception: Error: Cannot find module 'http://www.xxxxx.com/blabla/forza4.js'

So just to be on the safe side, I did:

new_game_obj = require('http://92.xx.xx.xx/blabla/forza4.js'); 

But again, the same error.

Should there be a problem loading an "extension" to my code from a remote server, or am I just formatting the "require" call wrong?

Thanks a bunch!

Yuval.

P.S. This is a follow up to this thread: This is the older and resolved post

Community
  • 1
  • 1
Yuval
  • 149
  • 1
  • 1
  • 11

2 Answers2

10

Take a look at the node.js modules docs

Specifically, refer to the require algorithm

Within node.js, require calls are synchronous, so it's not possible to load files that are not on your file system (ie, from an external url).

Update

You could fetch the code through an http request - or, even better, an https request and run it with the built-in vm module - or even with eval, but that seems not a good idea - as suggested on this old question.

Something like

https.get( 'https://www.xxxxx.com/blabla/forza4.js', function( res ){
  res.on( 'data', function( data ){
    vm.runInThisContext( data, 'remote/forza4.js' );
  });
});

Note: I did not test this code

Sure it isn't the best solution, but is a solution.

Community
  • 1
  • 1
laconbass
  • 17,080
  • 8
  • 46
  • 54
  • OK, I can accept that, it makes perfect sense, but it puts me on the spot, it means that if I need my main code to call into it a code from a different source I would have to put my outside file in the same file system that runs the main code... My problem start where the server farm our production server is being held captive they said they can't run the nodejs code on our production machine, so they take my code and run it as a process on a different Linux machine that is on the same local group with our server, and my socket connections request are being rerouted to the other machine... – Yuval May 09 '14 at 19:43
  • @Yuval I updated my answer with some info that may help you work-around with this. – laconbass May 10 '14 at 17:16
  • Please read carefully the answer and note I did not test that code, It's just an example to ilustrate the words. I can't help you without actually knowing what you want to do, please consider adding a new question and I will take a look at it. – laconbass Feb 03 '16 at 21:40
  • http://stackoverflow.com/questions/35188586/nodejs-how-to-load-remote-modules-with-auth – dod_basim Feb 04 '16 at 10:34
0

How about the 'require from URL' NPM package?

Just found it - not tried it, yet! https://www.npmjs.com/package/require-from-url

Ian Grainger
  • 5,148
  • 3
  • 46
  • 72