Skip to content

Package JSON

Helper functions for working with package.json files.

function getPackageJSON(filePathOrDirPath): Promise<Record<string, unknown>>;

Defined in: functions/packageJSON.ts:27

Helper function to asynchronously get a “package.json” file as an object.

Parameter Type Description
filePathOrDirPath string | undefined Either the path to a “package.json” file or the path to a directory which contains a “package.json” file. If undefined is passed, the current working directory will be used.

Promise<Record<string, unknown>>

If the “package.json” file cannot be found or is otherwise invalid.


function getPackageJSONDependencies(
filePathOrDirPathOrRecord,
dependencyType?,
): Promise<Record<string, string> | undefined>;

Defined in: functions/packageJSON.ts:55

Helper function to asynchronously get the “dependencies” or “devDependencies” or “peerDependencies” field from a “package.json” file. If the corresponding field does not exist, undefined will be returned.

Parameter Type Default value Description
filePathOrDirPathOrRecord string | Readonly<Record<string, unknown>> | undefined undefined Either the path to a “package.json” file, the path to a directory which contains a “package.json” file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used.
dependencyType DependencyType "dependencies" Optional. The specific dependencies field to get. Defaults to “dependencies”.

Promise<Record<string, string> | undefined>

If the “package.json” file cannot be found or is otherwise invalid.


function getPackageJSONField(
filePathOrDirPathOrRecord,
fieldName,
): Promise<string | undefined>;

Defined in: functions/packageJSON.ts:101

Helper function to asynchronously get an arbitrary string field from a “package.json” file. If the field does not exist, undefined will be returned.

Parameter Type Description
filePathOrDirPathOrRecord string | Readonly<Record<string, unknown>> | undefined Either the path to a “package.json” file, the path to a directory which contains a “package.json” file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used.
fieldName string The name of the field to retrieve.

Promise<string | undefined>

If the “package.json” file cannot be found or the field is not a string.


function getPackageJSONFieldMandatory(
filePathOrDirPathOrRecord,
fieldName,
): Promise<string>;

Defined in: functions/packageJSON.ts:139

Helper function to asynchronously get an arbitrary string field from a “package.json” file. This will throw an error if the “package.json” file cannot be found or the field does not exist or if the field is not a string.

Parameter Type Description
filePathOrDirPathOrRecord string | Readonly<Record<string, unknown>> | undefined Either the path to a “package.json” file, the path to a directory which contains a “package.json” file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used.
fieldName string The name of the field to retrieve.

Promise<string>


function getPackageJSONFieldsMandatory<T>(
filePathOrDirPath,
...fieldNames
): Promise<Record<T, string>>;

Defined in: functions/packageJSON.ts:164

Helper function to asynchronously get N arbitrary string fields from a “package.json” file. This will throw an error if the “package.json” file cannot be found or any of the fields do not exist or any of the fields are not strings.

Type Parameter
T extends string
Parameter Type Description
filePathOrDirPath string | undefined Either the path to a “package.json” file or the path to a directory which contains a “package.json” file. If undefined is passed, the current working directory will be used.
fieldNames readonly T[] The names of the fields to retrieve.

Promise<Record<T, string>>


function getPackageJSONScripts(
filePathOrDirPathOrRecord,
): Promise<Record<string, string> | undefined>;

Defined in: functions/packageJSON.ts:198

Helper function to asynchronously get the “scripts” field from a “package.json” file. If the field does not exist, undefined will be returned.

Parameter Type Description
filePathOrDirPathOrRecord string | Readonly<Record<string, unknown>> | undefined Either the path to a “package.json” file, the path to a directory which contains a “package.json” file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used.

Promise<Record<string, string> | undefined>

If the “package.json” file cannot be found or is otherwise invalid.


function getPackageJSONVersion(filePathOrDirPathOrRecord): Promise<string>;

Defined in: functions/packageJSON.ts:244

Helper function to asynchronously get the “version” field from a “package.json” file. This will throw an error if the “package.json” file cannot be found or the “version” field does not exist or the “version” field is not a string.

If you want to allow for the “version” field to not exist, use the getPackageJSONField helper function instead.

Parameter Type Description
filePathOrDirPathOrRecord string | Readonly<Record<string, unknown>> | undefined Either the path to a “package.json” file, the path to a directory which contains a “package.json” file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used.

Promise<string>


function packageJSONHasCatalog(filePathOrDirPathOrRecord): Promise<boolean>;

Defined in: functions/packageJSON.ts:264

Parameter Type
filePathOrDirPathOrRecord string | Readonly<Record<string, unknown>> | undefined

Promise<boolean>

https://bun.com/docs/pm/catalogs


function packageJSONHasDependency(
filePathOrDirPathOrRecord,
dependencyName,
dependencyType?,
): Promise<boolean>;

Defined in: functions/packageJSON.ts:295

Helper function to asynchronously check for a “dependencies” or “devDependencies” or “peerDependencies” field from a “package.json” file.

Parameter Type Default value Description
filePathOrDirPathOrRecord string | Readonly<Record<string, unknown>> | undefined undefined Either the path to a “package.json” file, the path to a directory which contains a “package.json” file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used.
dependencyName string undefined The name of the dependency to check for.
dependencyType DependencyType "dependencies" Optional. The specific dependencies field to get. Defaults to “dependencies”.

Promise<boolean>

If the “package.json” file cannot be found or is otherwise invalid.


function packageJSONHasScript(
filePathOrDirPathOrRecord,
scriptName,
): Promise<boolean>;

Defined in: functions/packageJSON.ts:324

Helper function to asynchronously check if a “package.json” file has a particular script. This will throw an error if the “package.json” file cannot be found or is otherwise invalid.

Parameter Type Description
filePathOrDirPathOrRecord string | Readonly<Record<string, unknown>> | undefined Either the path to a “package.json” file, the path to a directory which contains a “package.json” file, or a parsed JavaScript object from a JSON file. If undefined is passed, the current working directory will be used.
scriptName string The name of the script to check for.

Promise<boolean>