三生石@ 2012-02-05 22:15 采纳率: 100%
浏览 566
已采纳

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

Is there a way to get the version set in package.json in a nodejs app? 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

转载于:https://stackoverflow.com/questions/9153571/is-there-a-way-to-get-version-from-package-json-in-nodejs-code

  • 写回答

14条回答 默认 最新

  • 七度&光 2012-06-01 17:51
    关注

    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.

    var pjson = require('./package.json');
    console.log(pjson.version);
    

    A warning, courtesy of @Pathogen:

    Doing this with browserify has security implications.
    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.
    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 your server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(13条)

报告相同问题?

悬赏问题

  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示