install-if-needed v1.0.4
install-if-needed
Installs the given list of modules and saves them into their respective fields in your nearest package.json. Dependencies that already exist in your package.json will be skipped.
var install = require('install-if-needed')
install({
dependencies: ['through2'],
devDependencies: ['tape@2.x', 'standard']
}, function(err) {
if (err)
console.error("There was an error installing.")
})You can pass { stdio: 'inherit' } to preserve logging and colors, acting like the usual npm install command.
Usage
install(opt[, cb])
Looks at package JSON and installs any of the specified dependencies that are not listed in their respective field.
cwdthe directory for the closest package.json, (defaultprocess.cwd())packageoptional package data, if not defined will search for closestpackage.jsonsavewhether to--save,--save-devand--save-optional(default true)dependenciesdependencies to installdevDependenciesdev dependencies to installoptionalDependenciesoptional dependencies to installcommandthe command to spawn when installing, defaults to'npm'
Other options are passed to spawn-npm-install.
On complete, cb is called with (err) status. All dependencies also accept a single string instead of an array.
Alternatively, opt can be a string or array, which is the same as listing it in dependencies.
//e.g.
// npm install tape --save
install('tape', done)Motivation
This helps build CLI tooling that auto-installs modules as needed. For example, a tool which stubs out an empty test file for tape:
#!/usr/bin/env node
var install = require('install-if-needed')
var fs = require('fs')
var template = fs.readFileSync(__dirname + '/template.js')
install({
devDependencies: 'tape'
}, function(err) {
if (err) throw err
fs.writeFile(process.argv[2], template)
})And the CLI might be as simple as:
quick-tape tests/simple.jsLicense
MIT, see LICENSE.md for details.
