以编程方式安装 npm 包,提供 --save-dev 标志 [英] Programmatically install a npm package, providing --save-dev flag

查看:49
本文介绍了以编程方式安装 npm 包,提供 --save-dev 标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它没有很好的文档记录,但是您可以将 npm 用作 Node.js 模块并在代码中调用命令.

It's not very well documented, but you can use npm as a Node.js module and call commands in the code.

我想捕获用户输入的所需包并以这种方式安装它们,并使用 --save-dev 标志将它们保存到包中.我试图在代码中启动并运行它,但没有成功,但无法找到将其保存到包文件的方法.

I want to capture user input for what packages are required and installing them this way and also save them to the package with the --save-dev flag. I've tried to no avail to get this up and running in code, with it installing but can't find a way to get it to save to the package file.

这是否可能,或者是否可以通过其他方式完成.欢迎并赞赏其他方法.

Is this even possible, or would it have be done another way. Alternate methods are welcome and appreciated.

var npm = require("npm")

npm.load({}, function (er) {
  if (er) return handlError(er)

  npm.commands.install(["titlecase"], function (err, data) {
    if (err) return console.error(err)
  })

})

推荐答案

有可能,flags 需要传递给 npm.load():

It is possible, flags need to be passed to npm.load():

var npm = require('npm');

npm.load({ 'save-dev': true }, function (err) {
    if (err) console.log(err);

    npm.commands.install(['lodash'], function (err, data) {
        if (err) return console.error(err)
    });
});

您有标志列表及其类型 这里.

You have the list of flags and their type here.

这篇关于以编程方式安装 npm 包,提供 --save-dev 标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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