Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install programmatically a NPM package providing its version

Tags:

node.js

npm

I found how to install npm packages programmatically and the code works fine:

var npm = require("npm");
npm.load({
    loaded: false
}, function (err) {
  // catch errors
  npm.commands.install(["my", "packages", "to", "install"], function (er, data) {
    // log the error or data
  });
  npm.on("log", function (message) {
    // log the progress of the installation
    console.log(message);
  });
});

If I want to install the first version of hello-world package, how can I do this in the NodeJS side, using npm module?

I know that I can use child process, but I want to choose the npm module solution.

like image 811
Ionică Bizău Avatar asked Dec 19 '13 16:12

Ionică Bizău


People also ask

How npm install package version?

npm allows you to use SemVer to specify the package version to install. You can use a caret (^) character to specify the latest minor version to install or a tilde (~) character to specify the latest patch version to install. The above command fetched the highest minor version of the package, under 20.

How do I automatically install npm dependencies?

to install the dependencies automatically , first of all list them manually in package. json file and run the npm install (sometimes sudo npm install ) command.

Does npm install update version?

npm update command: This npm command is used for updating the dependencies that are mention in the package. json file as well as install all the missing packages in the directory and also used for updating the current node version on the machine.


1 Answers

NPM NodeJS API is not well documented, but checking the code helps up.

Here we find the following string:

install.usage = "npm install"
              + "\nnpm install <pkg>"
              + "\nnpm install <pkg>@<tag>"
              + "\nnpm install <pkg>@<version>"
              + "\nnpm install <pkg>@<version range>"
              + "\nnpm install <folder>"
              + "\nnpm install <tarball file>"
              + "\nnpm install <tarball url>"
              + "\nnpm install <git:// url>"
              + "\nnpm install <github username>/<github project>"
              + "\n\nCan specify one or more: npm install ./foo.tgz bar@stable /some/folder"
              + "\nIf no argument is supplied and ./npm-shrinkwrap.json is "
              + "\npresent, installs dependencies specified in the shrinkwrap."
              + "\nOtherwise, installs dependencies from ./package.json."

My question is about the version, so we can do: [email protected] to install 0.0.1 version of hello-world.

var npm = require("npm");
npm.load({
    loaded: false
}, function (err) {
  // catch errors
  npm.commands.install(["[email protected]"], function (er, data) {
    // log the error or data
  });
  npm.on("log", function (message) {
    // log the progress of the installation
    console.log(message);
  });
});

I didn't test, but I am sure that we can use any format of the install.usage solutions.

I wrote a function that converts the dependencies object in an array that can be passed to the install function call.

dependencies:

{
   "hello-world": "0.0.1"
}

The function gets the path to the package.json file and returns an array of strings.

function createNpmDependenciesArray (packageFilePath) {
    var p = require(packageFilePath);
    if (!p.dependencies) return [];

    var deps = [];
    for (var mod in p.dependencies) {
        deps.push(mod + "@" + p.dependencies[mod]);
    }

    return deps;
}
like image 100
Ionică Bizău Avatar answered Oct 02 '22 14:10

Ionică Bizău