Get Version from package.json File in Node.js

Get Version from package.json File in Node.js

When working on a Node.js project, the package.json file holds important metadata about the application, including its version. Sometimes, you might need to access this version programmatically - whether to log it, use it in your application, or include it in the API responses. This tutorial demonstrates how to get version from package.json file in Node.js.

Fortunately, Node.js offers an easy and direct method to access and read the contents of the package.json file using require function. Suppose we have the following package.json file:

{
  "name": "app",
  "version": "1.0.0",
  "description": "App description"
}

In the Node.js script, read the package version using the following code:

const packageInfo = require('./package.json');

const version = packageInfo.version;
console.log(version);

Output:

1.0.0

Ensure that you adjust the path in the require statement to match the location of the script relative to the package.json file.

Leave a Comment

Cancel reply

Your email address will not be published.