node.js - bash Is there a way to get version from package.json in nodejs code?



7 Answers

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

process.env.npm_package_version

See package.json vars for more details.

npm get package.json version

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



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",
  ...
}



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.




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.




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.




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.




I made a useful code to get the parent module's package.json

function loadParentPackageJson() {
    if (!module.parent || !module.parent.filename) return null
    let dir = path.dirname(module.parent.filename)
    let maxDepth = 5
    let packageJson = null
    while (maxDepth > 0) {
        const packageJsonPath = `${dir}/package.json`
        const exists = existsSync(packageJsonPath)
        if (exists) {
            packageJson = require(packageJsonPath)
            break
        }
        dir = path.resolve(dir, '../')
        maxDepth--
    }
    return packageJson
}





Related