Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to import a script from an URL using node.js?

Tags:

node.js

I'm currently trying to import one of my scripts from an URL, but the require function doesn't appear to be working in this case.

var functionChecker = require("http://javascript-modules.googlecode.com/svn/functionChecker.js");

This is an excerpt of the error message that was produced by this script:

Error: Cannot find module 'http://javascript-modules.googlecode.com/svn/functionChecker.js'

Is there any way to import a script from an URL in node.js?

like image 475
Anderson Green Avatar asked Oct 22 '22 20:10

Anderson Green


1 Answers

I finally got it to work. This example downloads the file http://javascript-modules.googlecode.com/svn/functionChecker.js, and then saves it in a local directory.

//var functionChecker = require(__dirname + '/functionChecker.js');
//functionChecker.checkAllFunctions(__filename);

var http = require('http');
var fs = require('fs');
var google = http.createClient(80, 'www.google.com');
var request = google.request('GET', '/svn/functionChecker.js',
  {'host': 'javascript-modules.googlecode.com'});
request.end();
out = fs.createWriteStream('functionChecker.js');
request.on('response', function (response) {
  response.setEncoding('utf8');
  response.on('data', function (chunk) {
    out.write(chunk);
  });
});

//function name: stuff
//requires functions: false
//is defined: false
//description: blah blah woohoo.
like image 172
Anderson Green Avatar answered Oct 27 '22 09:10

Anderson Green