Skip to content

Commit 38767b4

Browse files
aduh95ruyadorno
authored andcommitted
lib: do not throw if global property is no longer configurable
Fixes: #45336 PR-URL: #45344 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 4de67d1 commit 38767b4

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

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

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

179-
// Disable the current getter/setter and set up a new
180-
// non-enumerable property.
181-
deleteobject[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
returnlib;
191194
},

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,12 @@ child.exec(
354354
common.mustSucceed((stdout)=>{
355355
assert.match(stdout,/^number/);
356356
}));
357+
358+
// Regression test for https://github.com/nodejs/node/issues/45336
359+
child.execFile(process.execPath,
360+
['-p',
361+
'Object.defineProperty(global, "fs",{configurable: false });'+
362+
'fs === require("node:fs")'],
363+
common.mustSucceed((stdout)=>{
364+
assert.match(stdout,/^true/);
365+
}));

0 commit comments

Comments
(0)