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
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts