From 2c595da5dc934c5faf55031e34e3445554ac8d11 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 17 Nov 2022 13:17:48 -0500 Subject: [PATCH] lib: do not throw if global property is no longer configurable Fixes: https://github.com/nodejs/node/issues/45336 PR-URL: https://github.com/nodejs/node/pull/45344 Reviewed-By: Joyee Cheung Reviewed-By: Geoffrey Booth Reviewed-By: Jacob Smith Reviewed-By: James M Snell --- lib/internal/modules/cjs/helpers.js | 23 +++++++++++++---------- test/parallel/test-cli-eval.js | 9 +++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/internal/modules/cjs/helpers.js b/lib/internal/modules/cjs/helpers.js index 3c49ab0a5d84cb..fea3fbcf48dc00 100644 --- a/lib/internal/modules/cjs/helpers.js +++ b/lib/internal/modules/cjs/helpers.js @@ -176,16 +176,19 @@ function addBuiltinLibsToObject(object, dummyModuleName) { get: () => { const lib = dummyModule.require(name); - // Disable the current getter/setter and set up a new - // non-enumerable property. - delete object[name]; - ObjectDefineProperty(object, name, { - __proto__: null, - get: () => lib, - set: setReal, - configurable: true, - enumerable: false - }); + try { + // Override the current getter/setter and set up a new + // non-enumerable property. + ObjectDefineProperty(object, name, { + __proto__: null, + get: () => lib, + set: setReal, + configurable: true, + enumerable: false, + }); + } catch { + // If the property is no longer configurable, ignore the error. + } return lib; }, diff --git a/test/parallel/test-cli-eval.js b/test/parallel/test-cli-eval.js index e95fee008d6e3a..baa442e30ecd58 100644 --- a/test/parallel/test-cli-eval.js +++ b/test/parallel/test-cli-eval.js @@ -288,3 +288,12 @@ child.exec( common.mustSucceed((stdout) => { assert.strictEqual(stdout, '.mjs file\n'); })); + +// Regression test for https://github.com/nodejs/node/issues/45336 +child.execFile(process.execPath, + ['-p', + 'Object.defineProperty(global, "fs", { configurable: false });' + + 'fs === require("node:fs")'], + common.mustSucceed((stdout) => { + assert.match(stdout, /^true/); + }));