Node module for modules requirement.
Automatically tries to install the missing modules. Returns the modules under a callback or a promise.
npm install --save zquire
Example with Express
If 'express'
is already installed, it will require and return it. If it is not installed, it will install it and return it in the callback.
var zquire = require('zquire');
zquire('express', function(err, express) {
if (err) {
throw err;
} else {
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
}
});
From Express Hello World example
If you only give a callback to zquire
, it will deduce the modules you require with the arguments of your callback by parsing your callback arguments
var zquire = require('zquire');
zquire(function(err, express, path, fs, npm) {
if (err) {
throw err;
} else {
// Use expres, path, fs & npm here
}
});
zquire
includes es6-promise polyfill to allow the use of es6 promise.
var zquire = require('zquire');
zquire('express')
.then(function(express) {
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var server = app.listen(3000, function () {
var port = server.address().port;
console.log('Example app listening at http://localhost:%s', port);
});
})
.catch(function(err) {
throw err;
});
You can add npm CLI config params to zquire
. For example to save the module you require in your package.json
file if it needs to be installed.
More info about npm config params
var conf = {
save: true
};
//Callback's style
zquire('async', conf, function(err, async) {
if (!err) {
//use async here
}
});
//Promise's style
zquire('async', conf)
.then(function(async) {
//use async here
});
npm test