Get the version number from package.json in Node.js code

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Table of Contents

  1. Get the version number from package.json in Node.js code
  2. Using the npm_package_version environment variable to read the version from package.json
  3. Getting the version from package.json with a command
  4. Using the genversion package to get the version from your package.json file
  5. Using the fs module to get the version from package.json in Node.js

# Get the version number from package.json in Node.js code

You can import the package.json file to extract the version number from your package.json file in a Node.js script.

After you import the package.json file, access the version property to get the version number.

Suppose we have the following package.json file.

package.json
{ "version": "1.2.3" }

You can get the version number in a Node.js script as follows.

index.js
const packageJSON = require('./package.json'); console.log(packageJSON); console.log(packageJSON.version); // ๐Ÿ‘‰๏ธ 1.2.3

get version from package json in node

Alternatively, you can only import the version property from the object.

index.js
const {version} = require('./package.json'); console.log(version); // ๐Ÿ‘‰๏ธ 1.2.3
The code for this article is available on GitHub

The examples above use the CommonJS require() syntax.

If you use the ES6 modules import/export syntax, use the following code sample instead.

index.js
import packageJSON from './package.json' assert {type: 'json'}; console.log(packageJSON); console.log(packageJSON.version); // ๐Ÿ‘‰๏ธ 1.2.3
The code for this article is available on GitHub

I've written a detailed guide on how to import a JSON file in JavaScript and Node.js.

If you use a build tool (e.g. Babel), you can also try to use the following import statement.

index.js
import {version} from './package.json'; console.log(version);

The packageJSON variable stores the entire package.json object.

In order to get the version number from your package.json file, access the version property on the object.

The examples above assume that the package.json file and your index.js file are located in the same directory.

shell
my-project/ โ””โ”€โ”€ index.js โ””โ”€โ”€ package.json
The code for this article is available on GitHub

Make sure to update the import path if necessary.

Make sure to not expose more information than necessary, e.g. the version numbers of your dependencies, to untrusted users.

# Using the npm_package_version environment variable to read the version from package.json

You can also get the version from package.json by using the npm_package_version environment variable.

  1. Suppose we have the following package.json file.
package.json
{ "name": "bobbyhadz.com", "version": "1.2.3", "scripts": { "start": "node index.js" } }
  1. Here is how we'd access the version number in index.js.
index.js
console.log(process.env.npm_package_name); console.log(process.env.npm_package_version);
  1. Issue the npm run start command to run the start script from your package.json file.
shell
npm run start

get version from package json using env variable

The code for this article is available on GitHub
However, note that the npm_package_version environment variable won't be set if you don't run an npm script that is set in your package.json file.

For example, if you run the index.js file with node index.js, the environment variable won't be set.

Instead, add the command that you have to run to the scripts object of your package.json file and run it as npm run YOUR_SCRIPT.

# Getting the version from package.json with a command

If you need to get the version from your package.json file with a command:

  1. Open your shell in the root directory of your project (where your package.json file is).
  2. Run the following command.
shell
node -p "require('./package.json').version"

get version from package json with command

# Using the genversion package to get the version from your package.json file

You can also use the genversion package to get the version from your package.json file

The package is especially useful for client-side apps because it only generates a file that stores the version and won't expose any of the other properties in your package.json file.

  1. Open your terminal in your project's root directory (where your package.json file is).
  2. Install the genversion package.
shell
# ๐Ÿ‘‡๏ธ with NPM npm install genversion # Or with YARN yarn add genversion
  1. If you use the CommonJS require() syntax, run the following command.
shell
npx genversion version.js

The command will generate a version.js file that looks as follows.

version.js
// Generated by genversion. module.exports = '1.2.3'

If you use the ES6 import/export syntax, use the following command instead.

shell
npx genversion --es6 version.js

The command will generate a version.js file that looks as follows.

version.js
export const version = '1.2.3';
  1. Import the module as follows if you use CommonJS.
index.js
const version = require('./version'); console.log(version); // ๐Ÿ‘‰๏ธ 1.2.3

If you use the ES6 modules import/export syntax, import the module as follows.

index.js
import {version} from './version.js'; console.log(version); // ๐Ÿ‘‰๏ธ 1.2.3
The code for this article is available on GitHub

You can also generate the version.js file in a nested directory.

shell
# ๐Ÿ‘‡๏ธ CommonJS npx genversion lib/version.js # ๐Ÿ‘‡๏ธ ES6 Modules npx genversion --es6 lib/version.js

You can check more examples of using the genversion module in the package's NPM page.

# Using the fs module to get the version from package.json in Node.js

You can also use the fs module to get the version from package.json in Node.js.

index.js
import fs from 'fs'; const packageJSON = JSON.parse( fs.readFileSync('package.json', 'utf8'), ); console.log(packageJSON.version); // ๐Ÿ‘‰๏ธ 1.2.3

The example above uses the ES6 modules import/export syntax.

If you use the CommonJS require() syntax, use the following code sample instead.

index.js
const fs = require('fs'); const packageJSON = JSON.parse( fs.readFileSync('package.json', 'utf8'), ); console.log(packageJSON.version); // ๐Ÿ‘‰๏ธ 1.2.3
The code for this article is available on GitHub

We used the fs.readFileSync() method to read the contents of the package.json file and used the JSON.parse method to parse the JSON into a native JavaScript object.

The last step is to access the version property on the object to get the version number.

I've also written an article on how to set environment variables in package.json.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev