Last updated: Apr 5, 2024
Reading timeยท4 min
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.
{ "version": "1.2.3" }
You can get the version number in a Node.js script as follows.
const packageJSON = require('./package.json'); console.log(packageJSON); console.log(packageJSON.version); // ๐๏ธ 1.2.3
Alternatively, you can only import the version
property from the object.
const {version} = require('./package.json'); console.log(version); // ๐๏ธ 1.2.3
The examples above use the CommonJS require()
syntax.
If you use the ES6 modules import/export syntax, use the following code sample instead.
import packageJSON from './package.json' assert {type: 'json'}; console.log(packageJSON); console.log(packageJSON.version); // ๐๏ธ 1.2.3
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.
import {version} from './package.json'; console.log(version);
The packageJSON
variable stores the entire package.json
object.
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.
my-project/ โโโ index.js โโโ package.json
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.
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.
package.json
file.{ "name": "bobbyhadz.com", "version": "1.2.3", "scripts": { "start": "node index.js" } }
index.js
.console.log(process.env.npm_package_name); console.log(process.env.npm_package_version);
npm run start
command to run the start
script from your
package.json
file.npm run start
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
.
If you need to get the version from your package.json
file with a command:
package.json
file is).node -p "require('./package.json').version"
genversion
package to get the version from your package.json
fileYou 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.
package.json
file is).genversion
package.# ๐๏ธ with NPM npm install genversion # Or with YARN yarn add genversion
require()
syntax, run the following command.npx genversion version.js
The command will generate a version.js
file that looks as follows.
// Generated by genversion. module.exports = '1.2.3'
If you use the ES6 import/export syntax, use the following command instead.
npx genversion --es6 version.js
The command will generate a version.js
file that looks as follows.
export const version = '1.2.3';
const version = require('./version'); console.log(version); // ๐๏ธ 1.2.3
If you use the ES6 modules import/export syntax, import the module as follows.
import {version} from './version.js'; console.log(version); // ๐๏ธ 1.2.3
You can also generate the version.js
file in a nested directory.
# ๐๏ธ 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.
fs
module to get the version from package.json
in Node.jsYou can also use the fs
module to get the version from package.json
in
Node.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.
const fs = require('fs'); const packageJSON = JSON.parse( fs.readFileSync('package.json', 'utf8'), ); console.log(packageJSON.version); // ๐๏ธ 1.2.3
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.
You can learn more about the related topics by checking out the following tutorials: