我可以从在 Node.js 中运行的 javascript 安装 NPM 包吗? [英] Can I install a NPM package from javascript running in Node.js?

查看:19
本文介绍了我可以从在 Node.js 中运行的 javascript 安装 NPM 包吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从在 Node.js 中运行的 javascript 文件安装 NPM 包吗?例如,我想要一个脚本,我们称之为script.js",它以某种方式(...使用 NPM 或不...)安装通常可通过 NPM 获得的包.在这个例子中,我想安装FFI".(npm 安装 ffi)

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)

推荐答案

确实可以以编程方式使用 npm,并且在文档的较旧修订版中对此进行了概述.它已从官方文档中删除,但仍然存在于源代码管理中,并带有以下声明:

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:

虽然 npm 可以通过编程方式使用,但它的 API 是供仅 CLI,不保证其适用于任何其他目的.如果您想使用 npm 可靠地执行某些任务,最安全的做法是调用所需的 npm 命令适当的论据.

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.

npm 的语义版本指的是 CLI 本身,而不是底层 API.不保证内部 API 保持稳定即使 npm 的版本表明没有进行重大更改根据 semver.

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) { ... })
})

由于 npm 存在于 node_modules 文件夹中,您可以使用 require('npm') 像加载任何其他模块一样加载它.要安装模块,您需要使用 npm.commands.install().

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().

如果您需要查看源代码,那么它也在 GitHub 上.这是代码的完整工作示例,相当于在没有任何命令行参数的情况下运行 npm 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);
  });
});

注意 install 函数的第一个参数是一个数组.数组的每个元素都是一个 npm 将尝试安装的模块.

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.

更高级的使用可以在npm-cli.js 源代码管理文件.

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

这篇关于我可以从在 Node.js 中运行的 javascript 安装 NPM 包吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆