nx/docs/generated/devkit/ensurePackage.md
Juri Strumpflohner b51676a89a
docs(core): restructure guides into technologies sections (#31288)
Updates the docs structure, navigation etc to be easier + better suited
for showing Nx technology support beyond just TS.

**Notes:**

- API (`/nx-api`) tab is removed from the navigation (i.e. menu bar),
but pages still remain for now until we update references in `*.md`
files.
- Redirects are set up `/nx-api` to go to their respect new location
e.g. `/technologies` or `/reference/core-api`
- Old URLs still exist in the sitemap for now, but majority of them will
be redirected -- a follow-up PR can remove them.

**Preview:**
https://nx-dev-git-nx-dev-polyglot-docs-restructure-nrwl.vercel.app/docs

---------

Co-authored-by: Jack Hsu <jack.hsu@gmail.com>
2025-05-29 14:55:34 -04:00

63 lines
2.6 KiB
Markdown

# Function: ensurePackage
**ensurePackage**(`tree`, `pkg`, `requiredVersion`, `options?`): `void`
#### Parameters
| Name | Type | Description |
| :------------------------ | :-------------------------------------------------- | :----------------------------------------------------------------- |
| `tree` | [`Tree`](/reference/core-api/devkit/documents/Tree) | the file system tree |
| `pkg` | `string` | the package to check (e.g. @nx/jest) |
| `requiredVersion` | `string` | the 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:
```typescript
ensurePackage(tree, '@nx/jest', nxVersion);
```
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:
```typescript
ensurePackage('@nx/jest', nxVersion);
```
#### Type parameters
| Name | Type |
| :--- | :------------------------ |
| `T` | extends `unknown` = `any` |
#### Parameters
| Name | Type | Description |
| :-------- | :------- | :---------------------------------------------------------- |
| `pkg` | `string` | the package to install and require |
| `version` | `string` | the version to install if the package doesn't exist already |
#### Returns
`T`