Function: ensurePackage

ensurePackage(tree, pkg, requiredVersion, options?): void

Parameters

NameTypeDescription
treeTreethe file system tree
pkgstringthe package to check (e.g. @nx/jest)
requiredVersionstringthe version or semver range to check (e.g. ~1.0.0, >=1.0.0 <2.0.0)
options?Object-
options.dev?boolean-
options.throwOnMissing?boolean-

Returns

void

Deprecated

Use the other function signature without a Tree

Use a package that has not been installed as a dependency.

For example:

1ensurePackage(tree, '@nx/jest', nxVersion); 2

This install the @nx/jest@<nxVersion> and return the module When running with --dryRun, the function will throw when dependencies are missing. Returns null for ESM dependencies. Import them with a dynamic import instead.

ensurePackage<T>(pkg, version): T

Ensure that dependencies and devDependencies from package.json are installed at the required versions. Returns null for ESM dependencies. Import them with a dynamic import instead.

For example:

1ensurePackage('@nx/jest', nxVersion); 2

Type parameters

NameType
Textends unknown = any

Parameters

NameTypeDescription
pkgstringthe package to install and require
versionstringthe version to install if the package doesn't exist already

Returns

T