Skip to content

Commit 0ae9c1f

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular/cli): properly handle Node.js require() errors with ESM modules
Resolve `ERR_REQUIRE_ASYNC_MODULE` when attempting to `require()` ESM modules in a CommonJS context. Closes #30286
1 parent 30ac634 commit 0ae9c1f

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

packages/angular/build/src/utils/load-proxy-config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export async function loadProxyConfiguration(
6767
break;
6868
} catch (e) {
6969
assertIsError(e);
70-
if (e.code === 'ERR_REQUIRE_ESM') {
70+
if (e.code === 'ERR_REQUIRE_ESM' || e.code === 'ERR_REQUIRE_ASYNC_MODULE') {
7171
// Load the ESM configuration file using the TypeScript dynamic import workaround.
7272
// Once TypeScript provides support for keeping the dynamic import this workaround can be
7373
// changed to a direct dynamic import.

packages/angular_devkit/architect/node/node-modules-architect-host.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,10 @@ async function getBuilder(builderPath: string): Promise<any> {
324324
try {
325325
return localRequire(builderPath);
326326
} catch (e) {
327-
if ((e as NodeJS.ErrnoException).code === 'ERR_REQUIRE_ESM') {
327+
if (
328+
(e as NodeJS.ErrnoException).code === 'ERR_REQUIRE_ESM' ||
329+
(e as NodeJS.ErrnoException).code === 'ERR_REQUIRE_ASYNC_MODULE'
330+
) {
328331
// Load the ESM configuration file using the TypeScript dynamic import workaround.
329332
// Once TypeScript provides support for keeping the dynamic import this workaround can be
330333
// changed to a direct dynamic import.

packages/angular_devkit/build_angular/src/tools/webpack/configs/dev-server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ async function addProxyConfig(
210210
proxyConfiguration = require(proxyPath);
211211
} catch (e) {
212212
assertIsError(e);
213-
if (e.code !== 'ERR_REQUIRE_ESM') {
213+
if (e.code !== 'ERR_REQUIRE_ESM' && e.code !== 'ERR_REQUIRE_ASYNC_MODULE') {
214214
throw e;
215215
}
216216

packages/angular_devkit/build_webpack/src/utils.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ export async function getWebpackConfig(configPath: string): Promise<Configuratio
9090
try {
9191
return require(configPath);
9292
} catch (e) {
93-
if ((e as NodeJS.ErrnoException).code === 'ERR_REQUIRE_ESM') {
93+
if (
94+
(e as NodeJS.ErrnoException).code === 'ERR_REQUIRE_ESM' ||
95+
(e as NodeJS.ErrnoException).code === 'ERR_REQUIRE_ASYNC_MODULE'
96+
) {
9497
// Load the ESM configuration file using the TypeScript dynamic import workaround.
9598
// Once TypeScript provides support for keeping the dynamic import this workaround can be
9699
// changed to a direct dynamic import.

0 commit comments

Comments
 (0)