Skip to content

Commit

Permalink
fix(devkit): return null for ESM dependencies instead of throwing err…
Browse files Browse the repository at this point in the history
…or in ensurePackage (#17195)
  • Loading branch information
AgentEnder authored May 24, 2023
1 parent fe4682a commit 6d586dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
4 changes: 3 additions & 1 deletion docs/generated/devkit/nx_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ 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.

#### Parameters

Expand All @@ -1180,11 +1181,12 @@ When running with --dryRun, the function will throw when dependencies are missin
**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(tree, '@nx/jest', nxVersion);
ensurePackage('@nx/jest', nxVersion);
```

#### Type parameters
Expand Down
4 changes: 3 additions & 1 deletion docs/generated/packages/devkit/documents/nx_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ 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.

#### Parameters

Expand All @@ -1180,11 +1181,12 @@ When running with --dryRun, the function will throw when dependencies are missin
**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(tree, '@nx/jest', nxVersion);
ensurePackage('@nx/jest', nxVersion);
```

#### Type parameters
Expand Down
31 changes: 24 additions & 7 deletions packages/devkit/src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ const packageMapCache = new Map<string, any>();
* ```
* 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.
*
* @param tree the file system tree
* @param pkg the package to check (e.g. @nx/jest)
Expand All @@ -404,11 +405,13 @@ export function ensurePackage(

/**
* 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(tree, '@nx/jest', nxVersion)
* ensurePackage('@nx/jest', nxVersion)
* ```
*
* @param pkg the package to install and require
* @param version the version to install if the package doesn't exist already
*/
Expand Down Expand Up @@ -440,7 +443,11 @@ export function ensurePackage<T extends any = any>(
try {
return require(pkg);
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
if (e.code === 'ERR_REQUIRE_ESM') {
// The package is installed, but is an ESM package.
// The consumer of this function can import it as needed.
return null;
} else if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}
Expand Down Expand Up @@ -474,13 +481,23 @@ export function ensurePackage<T extends any = any>(
// Re-initialize the added paths into require
(Module as any)._initPaths();

const result = require(require.resolve(pkg, {
paths: [tempDir],
}));
try {
const result = require(require.resolve(pkg, {
paths: [tempDir],
}));

packageMapCache.set(pkg, result);
packageMapCache.set(pkg, result);

return result;
return result;
} catch (e) {
if (e.code === 'ERR_REQUIRE_ESM') {
// The package is installed, but is an ESM package.
// The consumer of this function can import it as needed.
packageMapCache.set(pkg, null);
return null;
}
throw e;
}
}

function addToNodePath(dir: string) {
Expand Down

1 comment on commit 6d586dd

@vercel
Copy link

@vercel vercel bot commented on 6d586dd May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.