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('.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 '.js'
So just to be on the safe side, I did:
new_game_obj = require('.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
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
Share Improve this question edited May 23, 2017 at 12:34 CommunityBot 11 silver badge asked May 9, 2014 at 13:49 YuvalYuval 1691 gold badge3 silver badges11 bronze badges 3- just for clarification, are you using requirejs.org? – laconbass Commented May 9, 2014 at 14:28
- Nope, I'm using Nodejs, and in the code I'm using the "require" action. – Yuval Commented May 9, 2014 at 14:52
- Ok, writing an answer for you now – laconbass Commented May 9, 2014 at 16:25
2 Answers
Reset to default 11Take 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.
How about the 'require from URL' NPM package?
Just found it - not tried it, yet! https://www.npmjs.com/package/require-from-url