When working with Node.js projects, retrieving the version from the package.json
file is a common requirement. Here, I will explore various methods below to get the version from package.json in Node.js.
Using require to Load package.json
This method utilizes the require
function to load the package.json
file and then extracts the version property from the loaded object.
var pjson = require('./package.json'); console.log(pjson.version);
Here, pjson
is an object representing the contents of package.json
, and pjson.version
accesses the version property.
Utilizing process.env when Launched with npm start
Accesses the version information stored in the npm_package_version
environment variable when the application is launched with npm start
.
const version = process.env.npm_package_version;
When your application is launched with npm start
, the version information is automatically available in the npm_package_version
environment variable. This method directly accesses the version from the environment.
ES6 Modules
Directly imports the version
property from package.json
using ES6 module syntax.
import { version } from './package.json';
This method uses ES6 import syntax to directly import the version
property from the package.json
file. Note that this approach is specific to projects using ES6 modules.
Shell Commands
These one-liners use Node.js in the command line to print the version without writing a script.
$ node -e "console.log(require('./package.json').version);" $ node -p "require('./package.json').version"
These one-liners are command-line expressions that use Node.js to evaluate JavaScript. They print the version directly from the package.json
file without writing a script.
NPM Commands
Uses npm commands to obtain the version information, with variations based on the npm version.
$ npm pkg get version $ npm -s run env echo '$npm_package_version' | xargs
These commands leverage npm to obtain the version information. The first command is available from npm v7.20.0 onwards, while the second one is for versions prior to that. The second command uses xargs
to remove quotes around the version.
Using pkginfo Module
Simplifies fetching information from package.json
using the pkginfo
module.
require('pkginfo')(module, 'version'); console.log(module.exports.version);
This method uses the pkginfo
module to simplify fetching information from package.json
. The version information is set to module.exports.version
.
Adding an NPM Script
Custom NPM script that generates a separate version file (version.js
) with a timestamp.
"scripts": { "build": "yarn version:output && blitz build", "version:output": "echo 'export const Version = { version: \"'$npm_package_version.$(date +%s)'\" }' > version.js" }
This involves adding a custom NPM script that generates a separate version file (version.js
). The version number is appended with a timestamp for uniqueness.
Handling ES6 Imports
For projects using ES6 imports (module type), this method uses fs/promises
to read package.json
asynchronously.
import { readFile } from 'fs/promises'; const pkg = JSON.parse(await readFile(new URL('./package.json', import.meta.url))); console.log(pkg.version);
This method is suitable for projects using ES6 imports (module type). It uses the fs/promises
module to read the package.json
file asynchronously and then extracts the version property from the parsed JSON.
Each method provides a different approach to retrieving the version from package.json
, and the choice depends on the project’s structure, requirements, and personal preferences.