本文翻译自:Is there a way to get version from package.json in nodejs code?
Is there a way to get the version set in package.json
in a nodejs app? 有没有办法在nodejs应用程序中获取package.json
中的版本集? I would want something like this 我想要这样的东西
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
#1楼
参考:https://stackoom.com/question/cPGF/有没有办法从nodejs代码中的package-json获取版本
#2楼
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
#3楼
I found that the following code fragment worked best for me. 我发现以下代码片段对我来说效果最好。 Since it uses require
to 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. 使用Browserify执行此操作具有安全隐患。
Be careful not to expose yourpackage.json
to the client, as it means that all your dependency version numbers, build and test commands and more are sent to the client. 注意不要将package.json
暴露给客户端,因为这意味着所有依赖版本号,构建和测试命令等都会发送到客户端。
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. 攻击者可以使用此类特定数据来更好地适应您服务器上的攻击。
#4楼
There is another way of fetching certain information from your package.json
file namely using pkginfo module. 还有另一种从package.json
文件中获取某些信息的方法,即使用pkginfo模块。
Usage of this module is very simple. 使用这个模块非常简单。 You can get all package variables using: 您可以使用以下方法获取所有包变量
require('pkginfo')(module);
Or only certain details ( version
in 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 (eg in subfolders) and it will automatically fetch information from your package.json
. 该模块具有非常好的功能 - 它可以在项目的任何文件中使用(例如在子文件夹中),它将自动从package.json
获取信息。 So you do not have to worry where you package.json
is. 所以你不必担心package.json
位置。
I hope that will help. 我希望这会有所帮助。
#5楼
If your application is launched with 'npm start', you can simply use: 如果你的应用程序是使用'npm start'启动的,你可以简单地使用:
process.env.npm_package_version
See package.json vars for more details. 有关更多详细信息,请参阅package.json vars 。
#6楼
I do this with findup-sync
: 我用findup-sync
执行此findup-sync
:
var findup = require('findup-sync');
var packagejson = require(findup('package.json'));
console.log(packagejson.version); // => '0.0.1'