Skip to content

Instantly share code, notes, and snippets.

@jonschlinkert
Last active February 3, 2018 05:58
Show Gist options
  • Save jonschlinkert/f4457fe39070894ca8637c85f59995cd to your computer and use it in GitHub Desktop.
Save jonschlinkert/f4457fe39070894ca8637c85f59995cd to your computer and use it in GitHub Desktop.

Install

Install with npm:

$ npm install --save is-installed

Why use this?

  • Useful for setup in unit tests, to conditionally install packages that aren't already installed.
  • Useful for validating that a global CLI is installed, and that it satisfies a valid semver range (like gulp-cli etc)

Usage

var isInstalled = require('is-installed');

console.log(isInstalled('foo'));
//=> false

API

Returns true if npm package name is installed in node_modules relative to the user's current working directory.

Params

  • name {String}: The name of the module to look for.
  • cwd {String|Function}: Optionally specify a directory to start the search from, or a validation function.
  • validate {Function}: Optionally specify a validation function to use as the last argument.
  • returns {Boolean}

Example

isInstalled('gulp');

// or with a validation function
var semver = require('semver');
var installed = isInstalled('gulp', function(pkg) {
  return semver.gt(pkg.version, '0.1.0');
});

Returns true if npm package name is installed in node_modules in the global npm directory.

Params

  • name {String}: The name of the module to look for.
  • cwd {String|Function}: Optionally specify a directory to start the search from, or a validation function.
  • validate {Function}: Optionally specify a validation function to use as the last argument.
  • returns {Boolean}

Example

isInstalled.global('gulp-cli');

// or with a validation function
var semver = require('semver');
var installed = isInstalled.global('gulp-cli', function(pkg) {
  return semver.gt(pkg.version, '0.1.0');
});
'use strict';
var path = require('path');
var gm = require('global-modules');
var exists = require('fs-exists-sync');
var findPkg = require('find-pkg');
var extend = require('extend-shallow');
/**
* Returns true if npm package `name` is installed in `node_modules` relative
* to the user's current working directory.
*
* ```js
* isInstalled('gulp');
*
* // or with a validation function
* var semver = require('semver');
* var installed = isInstalled('gulp', function(pkg) {
* return semver.gt(pkg.version, '0.1.0');
* });
* ```
* @param {String} `name` The name of the module to look for.
* @param {String|Function} `cwd` Optionally specify a directory to start the search from, or a validation function.
* @param {Function} `validate` Optionally specify a validation function to use as the last argument.
* @return {Boolean}
* @api public
*/
function isInstalled(name, cwd, validate) {
if (typeof cwd === 'function') {
validate = cwd;
cwd = null;
}
if (typeof cwd !== 'string') {
var userPkg = findPkg.sync(process.cwd());
cwd = userPkg ? path.dirname(userPkg) : process.cwd();
}
var mainPath = tryResolve(name, cwd);
if (!mainPath) {
return false;
}
var moduleDir = path.dirname(mainPath);
if (mainPath && typeof validate === 'function') {
var pkgPath = findPkg.sync(moduleDir);
var pkg = pkgPath ? require(pkgPath) : null;
return validate(pkg);
}
return true;
};
/**
* Returns true if npm package `name` is installed in `node_modules` in the
* global npm directory.
*
* ```js
* isInstalled.global('gulp-cli');
*
* // or with a validation function
* var semver = require('semver');
* var installed = isInstalled.global('gulp-cli', function(pkg) {
* return semver.gt(pkg.version, '0.1.0');
* });
* ```
* @param {String} `name` The name of the module to look for.
* @param {String|Function} `cwd` Optionally specify a directory to start the search from, or a validation function.
* @param {Function} `validate` Optionally specify a validation function to use as the last argument.
* @return {Boolean}
* @api public
*/
isInstalled.global = function(name, fn) {
return isInstalled(name, gm, fn);
};
/**
* Expose `isInstalled`
*/
module.exports = isInstalled;
/**
* Try to resolve the path to the given module
*/
function tryResolve(name, cwd) {
try {
return require.resolve(name);
} catch (err) {}
try {
return require.resolve(path.resolve(cwd, name));
} catch (err) {}
return null;
}
'use strict';
require('mocha');
var assert = require('assert');
var npm = require('npm-install-global');
var isInstalled = require('./');
var semver = require('semver');
describe('is-installed', function() {
before(function(cb) {
if (!isInstalled.global('mocha')) {
npm.install('mocha', cb);
} else {
cb();
}
});
describe('api', function() {
it('should export a function', function() {
assert.equal(typeof isInstalled, 'function');
});
it('should expose a `.global` method', function() {
assert.equal(typeof isInstalled.global, 'function');
});
});
describe('isInstalled', function() {
it('should return true if a package is installed locally', function() {
assert(isInstalled('mocha'));
});
it('should return false if a package is not installed locally', function() {
assert(!isInstalled('fosososos'));
});
it('should return true if the module exists and the validation function returns true', function() {
var installed = isInstalled('mocha', function(pkg) {
return semver.gt(pkg.version, '0.1.0');
});
assert(installed);
});
it('should return false if the module exists but the validation function returns false', function() {
var installed = isInstalled('mocha', function(pkg) {
return semver.gt(pkg.version, '1000.1.0');
});
assert(!installed);
});
});
describe('isInstalled.global', function() {
it('should return true if a package is installed locally', function() {
assert(isInstalled.global('mocha'));
});
it('should return false if a package is not installed locally', function() {
assert(!isInstalled.global('fosososos'));
});
it('should return true if the module exists and the validation function returns true', function() {
var installed = isInstalled.global('mocha', function(pkg) {
return semver.gt(pkg.version, '0.1.0');
});
assert(installed);
});
it('should return false if the module exists but the validation function returns false', function() {
var installed = isInstalled.global('mocha', function(pkg) {
return semver.gt(pkg.version, '1000.1.0');
});
assert(!installed);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment