Skip to content

Commit 2c595da

Browse files
aduh95danielleadams
authored andcommittedJan 3, 2023
lib: do not throw if global property is no longer configurable
Fixes: #45336 PR-URL: #45344 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 8b44c61 commit 2c595da

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed
 

‎lib/internal/modules/cjs/helpers.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,19 @@ function addBuiltinLibsToObject(object, dummyModuleName) {
176176
get: () => {
177177
const lib = dummyModule.require(name);
178178

179-
// Disable the current getter/setter and set up a new
180-
// non-enumerable property.
181-
delete object[name];
182-
ObjectDefineProperty(object, name, {
183-
__proto__: null,
184-
get: () => lib,
185-
set: setReal,
186-
configurable: true,
187-
enumerable: false
188-
});
179+
try {
180+
// Override the current getter/setter and set up a new
181+
// non-enumerable property.
182+
ObjectDefineProperty(object, name, {
183+
__proto__: null,
184+
get: () => lib,
185+
set: setReal,
186+
configurable: true,
187+
enumerable: false,
188+
});
189+
} catch {
190+
// If the property is no longer configurable, ignore the error.
191+
}
189192

190193
return lib;
191194
},

‎test/parallel/test-cli-eval.js

+9
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,12 @@ child.exec(
288288
common.mustSucceed((stdout) => {
289289
assert.strictEqual(stdout, '.mjs file\n');
290290
}));
291+
292+
// Regression test for https://github.com/nodejs/node/issues/45336
293+
child.execFile(process.execPath,
294+
['-p',
295+
'Object.defineProperty(global, "fs", { configurable: false });' +
296+
'fs === require("node:fs")'],
297+
common.mustSucceed((stdout) => {
298+
assert.match(stdout, /^true/);
299+
}));

0 commit comments

Comments
 (0)
Please sign in to comment.