Skip to main content

Package JSON

Helper functions for working with package.json files.

Functions

getPackageJSON()

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

Helper function to synchronously get a "package.json" file as an object. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDescription
filePathOrDirPathundefined | stringEither 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

Record<string, unknown>

Defined in

packages/complete-node/src/functions/packageJSON.ts:29


getPackageJSONAsync()

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

Helper function to asynchronously get a "package.json" file as an object. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDescription
filePathOrDirPathundefined | stringEither 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

Promise<Record<string, unknown>>

Defined in

packages/complete-node/src/functions/packageJSON.ts:52


getPackageJSONDependencies()

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

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

This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDefault valueDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>undefinedEither 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.
dependencyTypeDependencyType"dependencies"Optional. The specific dependencies field to get. Defaults to "dependencies".

Returns

Record<string, string> | undefined

Defined in

packages/complete-node/src/functions/packageJSON.ts:81


getPackageJSONDependenciesAsync()

function getPackageJSONDependenciesAsync(filePathOrDirPathOrRecord, dependencyType): Promise<Record<string, string> | undefined>

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.

This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDefault valueDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>undefinedEither 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.
dependencyTypeDependencyType"dependencies"Optional. The specific dependencies field to get. Defaults to "dependencies".

Returns

Promise<Record<string, string> | undefined>

Defined in

packages/complete-node/src/functions/packageJSON.ts:110


getPackageJSONField()

function getPackageJSONField(filePathOrDirPathOrRecord, fieldName): string | undefined

Helper function to synchronously get an arbitrary string field from a "package.json" file. If the field does not exist, undefined will be returned. This will throw an error if the "package.json" file cannot be found or the field is not a string.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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.
fieldNamestringThe name of the field to retrieve.

Returns

string | undefined

Defined in

packages/complete-node/src/functions/packageJSON.ts:162


getPackageJSONFieldAsync()

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

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

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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.
fieldNamestringThe name of the field to retrieve.

Returns

Promise<string | undefined>

Defined in

packages/complete-node/src/functions/packageJSON.ts:208


getPackageJSONFieldMandatory()

function getPackageJSONFieldMandatory(filePathOrDirPathOrRecord, fieldName): string

Helper function to synchronously 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 the field is not a string.

Also see the getPackageJSONField function.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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.
fieldNamestringThe name of the field to retrieve.

Returns

string

Defined in

packages/complete-node/src/functions/packageJSON.ts:256


getPackageJSONFieldsMandatory()

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

Helper function to synchronously 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.

Also see the getPackageJSONFieldMandatory function.

Type Parameters

Type Parameter
T extends string

Parameters

ParameterTypeDescription
filePathOrDirPathundefined | stringEither 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.
...fieldNamesreadonly T[]The names of the fields to retrieve.

Returns

Record<T, string>

Defined in

packages/complete-node/src/functions/packageJSON.ts:284


getPackageJSONScripts()

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

Helper function to synchronously get the "scripts" field from a "package.json" file. If the field does not exist, undefined will be returned. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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

Record<string, string> | undefined

Defined in

packages/complete-node/src/functions/packageJSON.ts:315


getPackageJSONScriptsAsync()

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

Helper function to asynchronously get the "scripts" field from a "package.json" file. If the field does not exist, undefined will be returned. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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

Promise<Record<string, string> | undefined>

Defined in

packages/complete-node/src/functions/packageJSON.ts:366


getPackageJSONVersion()

function getPackageJSONVersion(filePathOrDirPathOrRecord): string

Helper function to synchronously 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 not existing, use the getPackageJSONField helper function instead.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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

string

Defined in

packages/complete-node/src/functions/packageJSON.ts:420


isPackageJSONDependency()

function isPackageJSONDependency(filePathOrDirPathOrRecord, ...dependencyNames): boolean

Helper function to synchronously check if a "package.json" file has a particular dependency. Both the "dependencies" and the "devDependencies" fields will be checked. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

This function is variadic, meaning that you can pass as many dependency names as you want to check for. This function will return true if one or more dependencies were found.

Parameters

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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.
...dependencyNamesreadonly string[]The name of the dependency to check for.

Returns

boolean

Defined in

packages/complete-node/src/functions/packageJSON.ts:458


packageJSONHasScript()

function packageJSONHasScript(filePathOrDirPathOrRecord, scriptName): boolean

Helper function to synchronously 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

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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.
scriptNamestringThe name of the script to check for.

Returns

boolean

Defined in

packages/complete-node/src/functions/packageJSON.ts:500


packageJSONHasScriptAsync()

function packageJSONHasScriptAsync(filePathOrDirPathOrRecord, scriptName): Promise<boolean>

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

ParameterTypeDescription
filePathOrDirPathOrRecordundefined | string | Readonly<Record<string, unknown>>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.
scriptNamestringThe name of the script to check for.

Returns

Promise<boolean>

Defined in

packages/complete-node/src/functions/packageJSON.ts:531


setPackageJSONDependency()

function setPackageJSONDependency(
filePathOrDirPath,
dependencyName,
version,
dependencyType): void

Helper function to synchronously set a dependency in a "package.json" file to a new value. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDefault valueDescription
filePathOrDirPathundefined | stringundefinedEither 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.
dependencyNamestringundefinedThe name of the dependency to update.
versionstringundefinedThe new value for the dependency field. Note that most of the time, the version should have a "^" character prefix to indicate that patch updates should automatically be downloaded by the package manager.
dependencyTypeDependencyType"dependencies"Optional. The specific dependencies field to update. Defaults to "dependencies".

Returns

void

Defined in

packages/complete-node/src/functions/packageJSON.ts:566


setPackageJSONDependencyAsync()

function setPackageJSONDependencyAsync(
filePathOrDirPath,
dependencyName,
version,
dependencyType): Promise<void>

Helper function to asynchronously set a dependency in a "package.json" file to a new value. This will throw an error if the "package.json" file cannot be found or is otherwise invalid.

Parameters

ParameterTypeDefault valueDescription
filePathOrDirPathundefined | stringundefinedEither 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.
dependencyNamestringundefinedThe name of the dependency to update.
versionstringundefinedThe new value for the dependency field. Note that most of the time, the version should have a "^" character prefix to indicate that patch updates should automatically be downloaded by the package manager.
dependencyTypeDependencyType"dependencies"Optional. The specific dependencies field to update. Defaults to "dependencies".

Returns

Promise<void>

Defined in

packages/complete-node/src/functions/packageJSON.ts:603