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

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 斗士狗 2012-05-21 00:27

    Here is how to read the version out of package.json:

    fs = require('fs')
    json = JSON.parse(fs.readFileSync('package.json', 'utf8'))
    version = json.version
    
    评论
    解决 无用
    打赏 举报
  • 喵-见缝插针 2014-03-05 15:50

    There is another way of fetching certain information from your package.json file namely using pkginfo module.

    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)

    require('pkginfo')(module, 'version');
    

    And your package variables will be set to module.exports (so version number will be accessible via 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 (e.g. in subfolders) and it will automatically fetch information from your package.json. So you do not have to worry where you package.json is.

    I hope that will help.

    评论
    解决 无用
    打赏 举报
  • perhaps? 2014-03-12 00:00

    If your application is launched with 'npm start', you can simply use:

    process.env.npm_package_version
    

    See package.json vars for more details.

    评论
    解决 无用
    打赏 举报
  • YaoRaoLov 2015-01-18 15:15

    I do this with findup-sync:

    var findup = require('findup-sync');
    var packagejson = require(findup('package.json'));
    console.log(packagejson.version); // => '0.0.1' 
    
    评论
    解决 无用
    打赏 举报
  • 七度&光 2015-11-10 11:24

    Or in plain old shell:

    node -e "console.log(require('./package.json').version);"
    

    This can be shortened to

    node -p "require('./package.json').version"
    

    Even though this is not exactly what the question asked, it's useful if you want to use the version within package.json itself, for example to log to a versioned file in a script:

    {
      "name": "myapp",
      "version": "0.1.2",
      "scripts": {
        "run": "node index.js 2>&1 | tee -a myapp_v$(node -p \"require('./package.json').version\").log",
      ...
    }
    
    评论
    解决 无用
    打赏 举报
  • perhaps? 2015-12-23 19:27

    why not use the native way? the other methods have failed for me.

    // Load native UI library
    var gui = require('nw.gui');
    
    // Get the name field in manifest
    gui.App.manifest.version
    
    评论
    解决 无用
    打赏 举报
  • 三生石@ 2016-04-20 03:24

    Using ES6 modules you can do the following:

    import {version} from './package.json';
    
    评论
    解决 无用
    打赏 举报
  • ~Onlooker 2017-03-09 18:01

    I know this isn't the intent of the OP, but I just had to do this, so hope it helps the next person.

    If you're using docker-compose for your CI/CD process, you can get it this way!

    version:
      image: node:7-alpine
      volumes:
        - .:/usr/src/service/
      working_dir: /usr/src/service/
      command: ash -c "node -p \"require('./package.json').version.replace('\n', '')\""
    

    for the image, you can use any node image. I use alpine because it is the smallest.

    评论
    解决 无用
    打赏 举报
  • elliott.david 2017-08-02 09:13

    You can use the project-version package.

    $ npm install --save project-version
    

    Then

    const version = require('project-version');
    
    console.log(version);
    //=>  '1.0.0'
    

    It uses process.env.npm_package_version but fallback on the version written in the package.json in case the env var is missing for some reason.

    评论
    解决 无用
    打赏 举报
  • 三生石@ 2017-09-18 20:21

    Just adding an answer because I came to this question to see the best way to include the version from package.json in my web application.

    I know this question is targetted for Node.js however, if you are using Webpack to bundle your app just a reminder the recommended way is to use the DefinePlugin to declare a global version in the config and reference that. So you could do in your webpack.config.json

    const pkg = require('../../package.json');
    
    ...
    
    plugins : [
        new webpack.DefinePlugin({
          AppVersion: JSON.stringify(pkg.version),
    ...
    

    And then AppVersion is now a global that is available for you to use. Also make sure in your .eslintrc you ignore this via the globals prop

    评论
    解决 无用
    打赏 举报
  • 七度&光 2017-10-05 09:44

    For those who look for a safe client-side solution that also works on server-side, there is genversion. It is a command-line tool that reads the version from the nearest package.json and generates an importable CommonJS module file that exports the version. Disclaimer: I'm a maintainer.

    $ genversion lib/version.js
    

    I acknowledge the client-side safety was not OP's primary intention, but as discussed in answers by Mark Wallace and aug, it is highly relevant and also the reason I found this Q&A.

    评论
    解决 无用
    打赏 举报
  • The correct answers will be here in example:

    1. Answer (this code include package.json file and get version)

      const { version } = require('./package.json');


    1. Answer

      const version = process.env.npm_package_version


    Please don't use JSON.parse, fs.readFile, fs.readFileSync and don't use another npm modules it's not necessary for this question.

    评论
    解决 无用
    打赏 举报
  • Memor.の 2018-08-01 06:39

    Import your package.json file into your server.js or app.js and then access package.json properties into server file.

    var package = require('./package.json');
    

    package variable contains all the data in package.json.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题