以编程方式安装提供其版本的NPM软件包

以编程方式安装提供其版本的NPM软件包

本文介绍了以编程方式安装提供其版本的NPM软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了如何以编程方式安装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);
  });
});

如果我想安装hello-world软件包的第一个版本,如何使用npm模块在NodeJS端执行此操作?

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

我知道我可以使用子进程,但是我想选择npm模块解决方案.

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

推荐答案

NPM NodeJS API的文档不完善,但是检查代码会有所帮助.

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."

我的问题是关于版本的,所以我们可以做:[email protected]安装hello-world0.0.1版本.

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);
  });
});

我没有进行测试,但是我确定我们可以使用任何格式的install.usage解决方案.

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

我编写了一个函数,该函数将数组中的dependencies对象转换为可以传递给install函数调用的数组.

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"
}

该函数获取package.json文件的路径并返回字符串数组.

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;
}

这篇关于以编程方式安装提供其版本的NPM软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 12:37