THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial shows multiple ways to get a package.json version of a nodejs application.
There are multiple ways we can read package.json file in Nodejs Application.
First Way, using require and import, Second-way using fs module read json file.
if you are using the ES5 javascript version, use require keyword
var packageJson = require('./package.json');
console.log(packageJson.version);
use import keyword for ES6 Ecmapscript6 version
import {version} from './package.json';
console.log(packageJson.version);
Output:
1.1.4
It is an example of reading local json files using fs module, readFileSync function.
Here is an example code
fs = require('fs')
packagejson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
console.log(packagejson.version)
🧮 Tags
Recent posts
Multiple ways to iterate a loop with index and element in array in swift How to reload a page/component in Angular? How to populate enum object data in a dropdown in angular| Angular material dropdown example How to get the current date and time in local and UTC in Rust example Angular 13 UpperCase pipe tutorial | How to Convert a String to Uppercase exampleRelated posts