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

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

问题描述

我可以从Node.js中运行的javascript文件安装NPM包吗?例如,我想要一个脚本,让我们称它为script.js,不知何故(......使用NPM或不...)安装一个通常可以通过NPM获得的包。在这个例子中,我想安装FFI。 (npm install 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天全站免登陆