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 the package.json file in Nodejs Application.
First Way, using require and import, Second-way using the fs module read json file. You can also check other posts on npm command deprecate option is deprecated
if you are using the ES5 javascript version, use the required 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 the 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
Ways to skip test case execution in Gradle project build Learn Gradle | tutorials, and examples How to run only single test cases with Gradle build How to print dependency tree graph in Gradle projects How to perform git tasks with build script?Related posts