Can I install the NPM package from javascript running in Node.js?

Is it possible to install the NPM package from a javascript file running in Node.js? For example, I would like to have a script, call it "script.js", which somehow (... using NPM or not ...) installs a package, usually accessible through NPM. In this example, I would like to install "FFI". (npm install ffi)

+80
javascript npm
Apr 11 '13 at 19:45
source share
5 answers

Indeed, you can use npm programmatically, and this has been indicated in older versions of the documentation. It has since been removed from the official documentation, but still exists with respect to the control source with the following statement:

Although npm can be used programmatically, its API is designed to be used only by the CLI, and there is no guarantee as to its suitability for any other purpose. If you want to use npm to perform a specific task, the safest thing is to invoke the desired npm command with the appropriate arguments.

The semantic version of npm refers to the CLI itself, and not to the core API. The internal API is not guaranteed to remain stable even when the npm version indicates that no changes have been made according to semver.

The source documentation provides an example of the code that was provided:

var npm = require('npm') npm.load(myConfigObject, function (er) { if (er) return handlError(er) npm.commands.install(['some', 'args'], function (er, data) { if (er) return commandFailed(er) // command succeeded, and data might have some info }) npm.registry.log.on('log', function (message) { ... }) }) 

Since npm exists in the node_modules folder, you can use require('npm') to load it like any other module. To install the module, you will want to use npm.commands.install() .

If you need to look at the source, then also on GitHub . Here is a complete working code example that is equivalent to running npm install without command line arguments:

 var npm = require('npm'); npm.load(function(err) { // handle errors // install module ffi npm.commands.install(['ffi'], function(er, data) { // log errors or data }); npm.on('log', function(message) { // log installation progress console.log(message); }); }); 

Note that the first argument to the install function is an array. Each element of the array is a module that will try to install npm.

More advanced usage can be found in the npm-cli.js in the source control.

+100
Apr 11 '13 at
source share

Yes. you can use child_process to execute a system command

 var exec = require('child_process').exec, child; child = exec('npm install ffi', function (error, stdout, stderr) { console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); if (error !== null) { console.log('exec error: ' + error); } }); 
+23
Apr 11 '13 at
source share

if you want to get output, you can also use:

 var child_process = require('child_process'); child_process.execSync("npm install ffi",{stdio:[0,1,2]}); 

this way you can watch the installation, how you do it at hand, and avoid unpleasant surprises (buffer full, etc.)

+18
May 14 '17 at 10:14
source share

it may be a little light

 var exec = require('child_process').exec; child = exec('npm install ffi').stderr.pipe(process.stderr); 
+9
Oct. 16 '13 at 14:31
source share

I had a hell of a time trying to get the first example to work inside the project directory, posting here if anyone else finds this. As far as I can tell, NPM still works fine loaded directly, but since it assumes a CLI, we need to tweak it a bit:

 // this must come before load to set your project directory var previous = process.cwd(); process.chdir(project); // this is the part missing from the example above var conf = {'bin-links': false, verbose: true, prefix: project} // this is all mostly the same var cli = require('npm'); cli.load(conf, (err) => { // handle errors if(err) { return reject(err); } // install module cli.commands.install(['ffi'], (er, data) => { process.chdir(previous); if(err) { reject(err); } // log errors or data resolve(data); }); cli.on('log', (message) => { // log installation progress console.log(message); }); }); 
+5
Jul 15 '17 at 14:49
source share



All Articles