An easy way to find and read package json whether from a submodule or the application
npm install @libit/pkginfo> An easy way to find and read package json whether from a submodule or the application
```
$ npm install @libit/pkginfo
- Using pkg-up to find package json file
- Read package json file through fs.readFile instead of require for thisappinfo()
issue
- Provide and appinfo.sync() function to get application json data directly
- Throws more helpful JSON errors
- Normalizes the data
How can a module get package data from an application's package.json?
`ts
import {pkginfo} from '@libit/pkginfo';
// get applicaiton json data using pkginfo
console.log(await pkginfo(require.main));
// await pkginfo(require.main);
// =>
// [{"name": "application", ...}, '.../resolved-directory']
// or simply using appfino
console.log(await appinfo());
// await appinfo(require.main);
// =>
// [{"name": "application", ...}, '.../resolved-directory']
`
`ts
import {pkginfo} from '@libit/pkginfo';
console.log(await pkginfo());
// => [{name: '@libit/pkginfo', …}, '.../resolved-directory']
console.log(await pkginfo(require.main));
// => [{name: 'app', …}, '.../resolved-directory']
console.log(await pkginfo('some-other-directory'));
// => [{name: 'unicorn', …}, '.../resolved-directory']
`
Here's a sample of the output:
`js`
[
'.../fixtures',
{
name: 'simple-app',
description: 'A test fixture for pkginfo',
version: '0.1.0',
keywords: ['test', 'fixture'],
main: './dist/index.js',
scripts: {test: 'mocha __tests__/*/.test.js'},
engines: {node: '>= 12'},
},
];
Returns a Promise<[PackageJson, string]> or Promise<[NormalizePackageJson, string]> for package data and package
file directory, or Promise<[null, null]> if couldn't be found.
Returns a [PackageJson, string] or NormalizePackageJson, string for package data and package file directory, or
[null, null] if couldn't be found.
`ts``
appinfo() <=> pkginfo(require.main);
appinfo(false) <=> pkginfo(require.main, false);