Rush StackShopBlogEvents
Skip to main content

Home > @rushstack/node-core-library > PackageJsonLookup > loadOwnPackageJson

PackageJsonLookup.loadOwnPackageJson() method

A helper for loading the caller's own package.json file.


static loadOwnPackageJson(dirnameOfCaller: string): IPackageJson;


dirnameOfCallerstringThe NodeJS __dirname macro for the caller.



This function always returns a valid IPackageJson object. If any problems are encountered during loading, an exception will be thrown instead.


This function provides a concise and efficient way for an NPM package to report metadata about itself. For example, a tool might want to report its version.

The loadOwnPackageJson() probes upwards from the caller's folder, expecting to find a package.json file, which is assumed to be the caller's package. The result is cached, under the assumption that a tool's own package.json (and intermediary folders) will never change during the lifetime of the process.


// Report the version of our NPM package
const myPackageVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname).version;
console.log(`Cool Tool - Version ${myPackageVersion}`);