Function: getDependencyVersionFromPackageJson
▸ getDependencyVersionFromPackageJson(tree, packageName, packageJsonPath?, dependencyLookup?): string | null
Get the resolved version of a dependency from package.json.
Retrieves a package version and automatically resolves PNPM catalog references (e.g., "catalog:default") to their actual version strings. By default, searches dependencies first, then falls back to devDependencies.
Tree-based usage (generators and migrations): Use when you have a Tree object, which is typical in Nx generators and migrations.
Filesystem-based usage (CLI commands and scripts): Use when reading directly from the filesystem without a Tree object.
Parameters
| Name | Type | Description |
|---|---|---|
tree | Tree | - |
packageName | string | - |
packageJsonPath? | string | - |
dependencyLookup? | PackageJsonDependencySection[] | Array of dependency sections to check in order. Defaults to ['dependencies', 'devDependencies'] |
Returns
string | null
The resolved version string, or null if the package is not found in any of the specified sections
Example
1// Tree-based - from root package.json (checks dependencies then devDependencies)
2const reactVersion = getDependencyVersionFromPackageJson(tree, 'react');
3// Returns: "^18.0.0" (resolves "catalog:default" if present)
4
5// Tree-based - check only dependencies section
6const version = getDependencyVersionFromPackageJson(
7 tree,
8 'react',
9 'package.json',
10 ['dependencies']
11);
12
13// Tree-based - check only devDependencies section
14const version = getDependencyVersionFromPackageJson(
15 tree,
16 'jest',
17 'package.json',
18 ['devDependencies']
19);
20
21// Tree-based - custom lookup order
22const version = getDependencyVersionFromPackageJson(
23 tree,
24 'pkg',
25 'package.json',
26 ['devDependencies', 'dependencies', 'peerDependencies']
27);
28
29// Tree-based - with pre-loaded package.json
30const packageJson = readJson(tree, 'package.json');
31const version = getDependencyVersionFromPackageJson(
32 tree,
33 'react',
34 packageJson,
35 ['dependencies']
36);
37Example
1// Filesystem-based - from current directory
2const reactVersion = getDependencyVersionFromPackageJson('react');
3
4// Filesystem-based - with workspace root
5const version = getDependencyVersionFromPackageJson(
6 'react',
7 '/path/to/workspace'
8);
9
10// Filesystem-based - with specific package.json and section
11const version = getDependencyVersionFromPackageJson(
12 'react',
13 '/path/to/workspace',
14 'apps/my-app/package.json',
15 ['dependencies']
16);
17▸ getDependencyVersionFromPackageJson(tree, packageName, packageJson?, dependencyLookup?): string | null
Parameters
| Name | Type |
|---|---|
tree | Tree |
packageName | string |
packageJson? | PackageJson |
dependencyLookup? | PackageJsonDependencySection[] |
Returns
string | null
▸ getDependencyVersionFromPackageJson(packageName, workspaceRootPath?, packageJsonPath?, dependencyLookup?): string | null
Parameters
| Name | Type |
|---|---|
packageName | string |
workspaceRootPath? | string |
packageJsonPath? | string |
dependencyLookup? | PackageJsonDependencySection[] |
Returns
string | null
▸ getDependencyVersionFromPackageJson(packageName, workspaceRootPath?, packageJson?, dependencyLookup?): string | null
Parameters
| Name | Type |
|---|---|
packageName | string |
workspaceRootPath? | string |
packageJson? | PackageJson |
dependencyLookup? | PackageJsonDependencySection[] |
Returns
string | null