有没有办法从nodejs代码中的package.json获取版本?

本文翻译自: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 your package.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' 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值