Package JSON
Helper functions for working with package.json files.
Functions
Section titled “Functions”getPackageJSON()
Section titled “getPackageJSON()”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.
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<Record<string, unknown>>
Throws
Section titled “Throws”If the “package.json” file cannot be found or is otherwise invalid.
getPackageJSONDependencies()
Section titled “getPackageJSONDependencies()”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.
Parameters
Section titled “Parameters”| 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”. |
Returns
Section titled “Returns”Promise<Record<string, string> | undefined>
Throws
Section titled “Throws”If the “package.json” file cannot be found or is otherwise invalid.
getPackageJSONField()
Section titled “getPackageJSONField()”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.
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<string | undefined>
Throws
Section titled “Throws”If the “package.json” file cannot be found or the field is not a string.
getPackageJSONFieldMandatory()
Section titled “getPackageJSONFieldMandatory()”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.
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<string>
getPackageJSONFieldsMandatory()
Section titled “getPackageJSONFieldsMandatory()”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 Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends string |
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<Record<T, string>>
getPackageJSONScripts()
Section titled “getPackageJSONScripts()”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.
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<Record<string, string> | undefined>
Throws
Section titled “Throws”If the “package.json” file cannot be found or is otherwise invalid.
getPackageJSONVersion()
Section titled “getPackageJSONVersion()”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.
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<string>
packageJSONHasCatalog()
Section titled “packageJSONHasCatalog()”function packageJSONHasCatalog(filePathOrDirPathOrRecord): Promise<boolean>;Defined in: functions/packageJSON.ts:264
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
filePathOrDirPathOrRecord |
string | Readonly<Record<string, unknown>> | undefined |
Returns
Section titled “Returns”Promise<boolean>
https://bun.com/docs/pm/catalogs
packageJSONHasDependency()
Section titled “packageJSONHasDependency()”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.
Parameters
Section titled “Parameters”| 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”. |
Returns
Section titled “Returns”Promise<boolean>
Throws
Section titled “Throws”If the “package.json” file cannot be found or is otherwise invalid.
packageJSONHasScript()
Section titled “packageJSONHasScript()”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.
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<boolean>
