Load and parse package.json
and package-lock.json
files.
This library requires the following to run:
- Node.js 18+
Install with npm:
npm install @rowanmanning/package-json
Load into your code with import
or require
:
import { packageJson, packageLock } from '@rowanmanning/package-json';
// or
const { packageJson, packageLock } = require('@rowanmanning/package-json');
The following exports are available.
Load the package.json
file present in a given directory.
(path: string) => Promise<PackageJson>
The path
argument is required and must be an accessible folder that contains a package.json
file.
const pkg = await packageJson.fromDirectory('/path/to/my/repo');
// { name: 'my-package', version: '1.0.0', ... }
Load the package.json
file at a given file path.
(path: string) => Promise<PackageJson>
The path
argument is required and must be an accessible JSON file.
const pkg = await packageJson.fromFile('/path/to/my/repo/package.json');
// { name: 'my-package', version: '1.0.0', ... }
Parse a JSON string as a package.json
file.
(jsonString: string) => PackageJson
The jsonString
argument is required and must be valid JSON.
const pkg = await packageJson.fromString('{"name": "my-package", "version": "1.0.0"}');
// { name: 'my-package', version: '1.0.0' }
Verify whether a JavaScript object meets the basic standards for a package.json
file (e.g. has a name
and version
property).
(object: any) => PackageJson
The jsonString
argument is required and must be valid JSON.
const pkg = await packageJson.fromObject({
name: 'my-package',
version: '1.0.0'
});
// { name: 'my-package', version: '1.0.0' }
const pkg = await packageJson.fromObject({});
// Throws an error
Load the package-lock.json
file present in a given directory.
(path: string) => Promise<PackageLock>
The path
argument is required and must be an accessible folder that contains a package-lock.json
file.
const pkg = await packageLock.fromDirectory('/path/to/my/repo');
// { lockfileVersion: 3, name: 'my-package', version: '1.0.0', ... }
Load the package-lock.json
file at a given file path.
(path: string) => Promise<PackageLock>
The path
argument is required and must be an accessible JSON file.
const pkg = await packageLock.fromFile('/path/to/my/repo/package-lock.json');
// { lockfileVersion: 3, name: 'my-package', version: '1.0.0', ... }
Parse a JSON string as a package-lock.json
file.
(jsonString: string) => PackageLock
The jsonString
argument is required and must be valid JSON.
const pkg = await packageLock.fromString('{"lockfileVersion": 3, "name": "my-package", "version": "1.0.0"}');
// { lockfileVersion: 3, name: 'my-package', version: '1.0.0' }
Verify whether a JavaScript object meets the basic standards for a package-lock.json
file (e.g. has a valid lockfileVersion
, name
and version
property).
(object: any) => PackageLock
The jsonString
argument is required and must be valid JSON.
const pkg = await packageLock.fromObject({
lockfileVersion: 3,
name: 'my-package',
version: '1.0.0'
});
// { lockfileVersion: 3, name: 'my-package', version: '1.0.0' }
const pkg = await packageLock.fromObject({});
// Throws an error
See the central README for a contribution guide and code of conduct.
Licensed under the MIT license.
Copyright © 2024, Rowan Manning