Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Can I install a NPM package from a javascript file running in Node.js? For example, I'd like to have a script, let's call it "script.js" that somehow (...using NPM or not...) install a package usually available through NPM. In this example, I'd like to install "FFI". (npm install ffi)

like image 228
Justin Avatar asked Apr 11 '13 19:04

Justin


People also ask

Can we use npm package in JavaScript?

NPM is the default package manager for node. It is used to install, share, and manage javascript packages in a project.

Does node js also install npm?

The Node.js installer includes the NPM package manager. Note: There are other versions available. If you have an older system, you may need the 32-bit version.

Can I use JavaScript in node JS?

Node. js allows you to run JavaScript on the server.

Do I have to install Node JS to run JavaScript?

You can Run your JavaScript File from your Terminal only if you have installed NodeJs runtime. If you have Installed it then Simply open the terminal and type “node FileName. js”. If you don't have NodeJs runtime environment then go to NodeJs Runtime Environment Download and Download it.


2 Answers

It is indeed possible to use npm programmatically, and it was outlined in older revisions of the documentation. It has since been removed from the official documentation, but still exists on source control with the following statement:

Although npm can be used programmatically, its API is meant for use by the CLI only, and no guarantees are made regarding its fitness for any other purpose. If you want to use npm to reliably perform some task, the safest thing to do is to invoke the desired npm command with appropriate arguments.

The semantic version of npm refers to the CLI itself, rather than the underlying API. The internal API is not guaranteed to remain stable even when npm's version indicates no breaking changes have been made according to semver.

In the original documentation, the following is the code sample 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 a module, you will want to use npm.commands.install().

If you need to look in the source then it's also on GitHub. Here's a complete working example of the code, which is the equivalent of running npm install without any 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 npm will attempt to install.

More advanced use can be found in the npm-cli.js file on source control.

like image 126
hexacyanide Avatar answered Oct 12 '22 10:10

hexacyanide


You can use child_process.exec or execSync to spawn a shell then execute the desired command within that shell, buffering any generated output:

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

If a callback function is provided, it is called with the arguments (error, stdout, stderr). This way you can run the installation like you do it manualy and see the full output.

The child_process.execSync() method is generally identical to child_process.exec() with the exception that the method will not return until the child process has fully closed.

like image 28
krankuba Avatar answered Oct 12 '22 10:10

krankuba