node.js - 确定 npm 将为给定版本范围选择哪个版本的包

标签 node.js npm semantic-versioning

当我运行npm install react@^15时,它会自动选择满足^15的最大版本。我可以使用 CLI 命令或 Node API 来确定要安装的 npm 版本,而无需实际安装它吗?

我可以执行以下操作,但我正在寻找更简洁的内容:

const semver = require('semver')
const {exec} = require('child-process-async')

async function resolveVersion(pkg: string, range: string): Promise<?string> {
  const result = JSON.parse((await exec(`npm view --json ${pkg} verisons dist-tags`)).stdout)
  return result['dist-tags'][range] || semver.maxSatisfying(versions, range)
}

最佳答案

是的,您可以使用--dry-run标志:

The --dry-run argument will report in the usual way what the install would have done without actually installing anything.

示例:

> npm install react@^15 --dry-run

+ react@15.6.2
added 18 packages in 0.673s

关于node.js - 确定 npm 将为给定版本范围选择哪个版本的包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47542609/

相关文章:

node.js - 找不到模块 'npmlog' - Windows

javascript - 使用 VS 代码调试 Vue.js 应用程序。错误未验证断点

go - 向构造函数添加可变参数时如何在 Go 中进行版本控制?

javascript - 为语义发布的包生成变更日志

javascript - 如何将类属性传递给嵌套在 NodeJS 中 class>method>each 内的 .each block ?

node.js - NodeJs 与 Mongoose - 嵌套查询异步问题

node.js - Node.js 中奇怪的继承

node.js - 处理 Node.JS 中类的函数调用

node.js - 安装 Gulp 时出现问题

frontend - 如何对前端项目进行版本控制?