胖鸭 2012-02-05 22:15
浏览 1134
已采纳

有没有办法从 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

  • 写回答

3条回答 默认 最新

  • 北城已荒凉 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.

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?