Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

node.js - Install programmatically a NPM package providing its version

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Here we find the following string:

install.usage = "npm install"
              + "
npm install <pkg>"
              + "
npm install <pkg>@<tag>"
              + "
npm install <pkg>@<version>"
              + "
npm install <pkg>@<version range>"
              + "
npm install <folder>"
              + "
npm install <tarball file>"
              + "
npm install <tarball url>"
              + "
npm install <git:// url>"
              + "
npm install <github username>/<github project>"
              + "

Can specify one or more: npm install ./foo.tgz bar@stable /some/folder"
              + "
If no argument is supplied and ./npm-shrinkwrap.json is "
              + "
present, installs dependencies specified in the shrinkwrap."
              + "
Otherwise, installs dependencies from ./package.json."

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

var npm = require("npm");
npm.load({
    loaded: false
}, function (err) {
  // catch errors
  npm.commands.install(["hello-world@0.0.1"], 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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...