简体   繁体   English

获取未加载的Node软件包的版本号?

[英]Get the version number of an installed Node package without loading it?

It is possible to get the version of the current package via 可以通过获取当前软件包的版本

const { version } = require('./package.json')

but how can I get the version number of an arbitrary, installed package without loading it? 但是如何在不加载的情况下获取任意已安装软件包的版本号?

I found a solution with require.resolve : 我找到了一个require.resolve的解决方案:

const path = require('path')

// get version number without requiring the module
function packageVersion(moduleName) {
  const dir = path.dirname(require.resolve(moduleName))
  return require(path.join(dir, 'package.json')).version
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 启动时未加载最新安装的节点版本 - Latest installed node version not loading on startup 如何获取node.js软件包路径(无论安装在何处)及其名称和版本 - How to get the node.js package path(wherever installed) along with its name and version 安装与节点的已安装版本兼容的软件包的最新版本 - Install the latest version of a package that is compatible with the installed version of node 从节点脚本获取安装的npm版本? - Get installed version of npm from node script? 从未安装的节点 package 动态加载 javascript 文件 - Dynamically loading a javascript file from a non-installed node package 在不安装 package 的情况下更新到 Node 版本 16 - Update to Node version 16 without installing package 如何告诉节点使用特定的已安装软件包版本? - How can I tell node to use a particular installed package version? 'npm 是否安装'l<package_name> 安装一个package的最新版本还是安装最新版本的node兼容?</package_name> - Does 'npm instal'l <package_name> install the latest version of a package or the latest compatible with the version of node installed? 在 Nexus 中获取最新版本的节点 package - Get the latest version of a node package in Nexus Node.js:获取已安装的npm包的(绝对)根路径 - Node.js: Get (absolute) root path of installed npm package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM