有没有办法从 nodejs 代码中的 package.json 获取版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9153571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to get version from package.json in nodejs code?
提问by Abhik Bose Pramanik
Is there a way to get the version set in package.jsonin a nodejs app? I would want something like this
有没有办法package.json在 nodejs 应用程序中设置版本?我想要这样的东西
var port = process.env.PORT || 3000
app.listen port
console.log "Express server listening on port %d in %s mode %s", app.address().port, app.settings.env, app.VERSION
回答by Mark Wallace
I found that the following code fragment worked best for me. Since it uses requireto load the package.json, it works regardless the current working directory.
我发现以下代码片段最适合我。由于它用于require加载package.json,因此无论当前工作目录如何,它都可以工作。
var pjson = require('./package.json');
console.log(pjson.version);
A warning, courtesy of @Pathogen:
警告,由@Pathogen 提供:
Doing this with Browserify has security implications.
Be careful not to expose yourpackage.jsonto the client, as it means that all your dependency version numbers, build and test commands and more are sent to the client.
If you're building server and client in the same project, you expose your server-side version numbers too. Such specific data can be used by an attacker to better fit the attack on your server.
使用 Browserify 执行此操作具有安全隐患。
注意不要将您package.json的内容暴露给客户端,因为这意味着您的所有依赖项版本号、构建和测试命令等都将发送到客户端。
如果你在同一个项目中构建服务器和客户端,你也会公开你的服务器端版本号。攻击者可以使用此类特定数据来更好地适应对您服务器的攻击。
回答by Julien Christin
If your application is launched with 'npm start', you can simply use:
如果您的应用程序是通过“npm start”启动的,您只需使用:
process.env.npm_package_version
See package.json varsfor more details.
有关更多详细信息,请参阅package.json 变量。
回答by Patrick Lee Scott
Using ES6 modules you can do the following:
使用 ES6 模块,您可以执行以下操作:
import {version} from './package.json';
回答by abernier
Or in plain old shell:
或者在普通的旧外壳中:
node -e "console.log(require('./package.json').version);"
This can be shortened to
这可以缩短为
node -p "require('./package.json').version"
Even though this is not exactly what the question asked, it's useful if you want to use the version within package.jsonitself, for example to log to a versioned file in a script:
尽管这不是问题所问的确切内容,但如果您想在其内部使用该版本,它也很有用package.json,例如登录到以下文件中的版本化文件script:
{
"name": "myapp",
"version": "0.1.2",
"scripts": {
"run": "node index.js 2>&1 | tee -a myapp_v$(node -p \"require('./package.json').version\").log",
...
}
回答by аlex dyky?
There are two ways of retrieving the version:
有两种检索版本的方法:
- Requiring
package.jsonand getting the version:
- 要求
package.json和获取版本:
const { version } = require('./package.json');
- Using the environment variables:
- 使用环境变量:
const version = process.env.npm_package_version;
Please don't use JSON.parse, fs.readFile, fs.readFileSyncand don't use another npm modulesit's not necessary for this question.
请不要使用JSON.parse, fs.readFile,fs.readFileSync并且不要使用另一个npm modules对于这个问题没有必要的。
回答by Evan Moran
Here is how to read the version out of package.json:
以下是从 package.json 中读取版本的方法:
fs = require('fs')
json = JSON.parse(fs.readFileSync('package.json', 'utf8'))
version = json.version
回答by Tom
There is another way of fetching certain information from your package.jsonfile namely using pkginfomodule.
还有另一种从package.json文件中获取某些信息的方法,即使用pkginfo模块。
Usage of this module is very simple. You can get all package variables using:
这个模块的使用非常简单。您可以使用以下方法获取所有包变量:
require('pkginfo')(module);
Or only certain details (versionin this case)
或者只有某些细节(version在这种情况下)
require('pkginfo')(module, 'version');
And your package variables will be set to module.exports(so version number will be accessible via module.exports.version).
并且您的包变量将设置为module.exports(因此可以通过 访问版本号module.exports.version)。
You could use the following code snippet:
您可以使用以下代码片段:
require('pkginfo')(module, 'version');
console.log "Express server listening on port %d in %s mode %s", app.address().port, app.settings.env, module.exports.version
This module has very nice feature - it can be used in any file in your project (e.g. in subfolders) and it will automatically fetch information from your package.json. So you do not have to worry where you package.jsonis.
这个模块有一个非常好的特性——它可以用在你项目中的任何文件中(例如在子文件夹中),它会自动从你的package.json. 所以你不必担心你在哪里package.json。
I hope that will help.
我希望这会有所帮助。
回答by Akseli Palén
For those who look for a safe client-sidesolution that also works on server-side, there is genversion. It is a command-line tool that reads the version from the nearest package.json and generates an importable CommonJS module file that exports the version. Disclaimer: I'm a maintainer.
对于那些寻找也适用于服务器端的安全客户端解决方案的人来说,有genversion。它是一个命令行工具,从最近的 package.json 中读取版本并生成一个可导入的 CommonJS 模块文件来导出版本。免责声明:我是维护者。
$ genversion lib/version.js
I acknowledge the client-side safety was not OP's primary intention, but as discussed in answers by Mark Wallaceand aug, it is highly relevant and also the reason I found this Q&A.
我承认客户端安全不是 OP 的主要意图,但正如Mark Wallace和aug 的回答中所讨论的那样,它高度相关,也是我发现此问答的原因。
回答by aug
Just adding an answer because I came to this question to see the best way to include the version from package.json in my web application.
只是添加一个答案,因为我来到这个问题是为了查看将 package.json 中的版本包含在我的 Web 应用程序中的最佳方法。
I know this question is targetted for Node.js however, if you are using Webpack to bundle your app just a reminder the recommended way is to use the DefinePluginto declare a global version in the config and reference that. So you could do in your webpack.config.json
我知道这个问题是针对 Node.js 的,但是,如果您使用 Webpack 捆绑您的应用程序只是一个提醒,推荐的方法是使用DefinePlugin在配置中声明一个全局版本并引用它。所以你可以在你的webpack.config.json
const pkg = require('../../package.json');
...
plugins : [
new webpack.DefinePlugin({
AppVersion: JSON.stringify(pkg.version),
...
And then AppVersionis now a global that is available for you to use. Also make sure in your .eslintrcyou ignore this via the globals prop
然后AppVersion现在是一个可供您使用的全局变量。还要确保您.eslintrc通过 globals 道具忽略了这一点
回答by ángelBlanco
You can use ES6 to import package.json to retrieve version number and output the version on console.
您可以使用 ES6 导入 package.json 来检索版本号并在控制台上输出版本。
import {name as app_name, version as app_version} from './path/to/package.json';
console.log(`App ---- ${app_name}\nVersion ---- ${app_version}`);

