diff --git a/src/node_contextify.cc b/src/node_contextify.cc index ec0f68671301d0..85cdd4b3e74163 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1468,6 +1468,7 @@ void ContextifyContext::ContainsModuleSyntax( String::NewFromUtf8(isolate, "__dirname").ToLocalChecked()}; TryCatchScope try_catch(env); + ShouldNotAbortOnUncaughtScope no_abort_scope(env); ContextifyContext::CompileFunctionAndCacheResult(env, context, diff --git a/test/es-module/test-esm-detect-ambiguous.mjs b/test/es-module/test-esm-detect-ambiguous.mjs index 34c5f17f007c8f..43537f26f5304a 100644 --- a/test/es-module/test-esm-detect-ambiguous.mjs +++ b/test/es-module/test-esm-detect-ambiguous.mjs @@ -235,3 +235,23 @@ describe('--experimental-detect-module', { concurrency: true }, () => { } }); }); + +// Validate temporarily disabling `--abort-on-uncaught-exception` +// while running `containsModuleSyntax`. +// Ref: https://github.com/nodejs/node/issues/50878 +describe('Wrapping a `require` of an ES module while using `--abort-on-uncaught-exception`', () => { + it('should work', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--abort-on-uncaught-exception', + '--eval', + 'assert.throws(() => require("./package-type-module/esm.js"), { code: "ERR_REQUIRE_ESM" })', + ], { + cwd: fixtures.path('es-modules'), + }); + + strictEqual(stderr, ''); + strictEqual(stdout, ''); + strictEqual(code, 0); + strictEqual(signal, null); + }); +});