Is it possible to automatically download the required modules for a node.js script? I'm wondering if it's possible to generate a list of required modules for a node.js script (like the one below), and install them automatically, instead of installing them manually, one-by-one (using npm).
#!/usr/bin/env node
var DNode = require('dnode');
var sys = require('sys');
var fs = require('fs');
var http = require('http');
var html = fs.readFileSync(__dirname + '/web.html');
var js = require('dnode/web').source();
//the rest of this script is omitted.
Yes, there is a great piece of code called NPM for exactly this: https://npmjs.org/
You specify dependent packages in a package.json file (see the docs for syntax) and you can use npm install . to pull them in all at once, and then require them from your script.
Package.json syntax page: https://npmjs.org/doc/json.html
I writed a script for that.
(function(){
var r=require;
require=function (n){
try{
return r(n)
}
catch(e){
r('child_process').exec('npm i ' + n,function (err,body){
try{
console.log('Module "' +n + '"" not found, try to install. Please restart the app\n' + body )
return r(n);
}
catch(e){
}
})
}
}
})()