Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @kev_nz/npm-programmatic with all npm packages installed. Try it out:

var npmProgrammatic = require("@kev_nz/npm-programmatic")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@kev_nz/npm-programmatic v0.0.8

Use NPM commands programmatically

npm-programmatic Build Status

NPM

npm-programmatic is a library that allows you to access npm commands programmatically from javascript. This is a fork that fixes an issue with saving dev dependencies. If you don't need to save dev dependencies I do recommend the main version.

Usage

Every function returns a Bluebird promise.
CWD refers to current working directory, allowing you to ensure the command executes in a certain folder in the filesystem. If output is set, the output of npm will be shown in the console.

Installation of Packages

    npm.install(packages, opts).then(function)
NameTypeValue
packagesArraypackages to be installed
optsObjectsave:true/false; global:true/false; cwd:string; saveDev:true/false; output:true/false

Example

    var npm = require('npm-programmatic');
    npm.install(['left-pad'], {
        cwd:'/path/to/my/project',
        saveDev:true
    })
    .then(function(){
        console.log("SUCCESS!!!");
    })
    .catch(function(){
        console.log("Unable to install package");
    });

Unistallation of Packages

    npm.uninstall(packages, opts).then(function)
NameTypeValue
packagesArraypackages to be uninstalled
optsObjectsave:true/false; global:true/false; cwd:string; saveDev:true/false; output:true/false

Example

    var npm = require('npm-programmatic');
    npm.uninstall(['left-pad'], {
        cwd:'/path/to/my/project',
        save:true
    })
    .then(function(){
        console.log("SUCCESS!!!");
    })
    .catch(function(){
        console.log("Unable to uninstall package");
    });

List Installed Packages

    npm.list(path).then(function)
NameTypeValue
pathStringpath at which to look

Example

    var npm = require('npm-programmatic');
    npm.list('/path/to/project')
    .then(function(arrayOfPackages){
        console.log(arrayOfPackages);
    })
    .catch(function(){
        console.log("Unable to uninstall package");
    });

Tests

install mocha and dev dependencies. Then run npm test

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free