csdnceshi62 2012-02-05 22:15 采纳率: 100%
浏览 1110
已采纳

有没有办法从 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条回答 默认 最新

  • larry*wei 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 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?