From 391aeb1996330886c671f0946377a03365e87af7 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 19 Jan 2024 09:42:07 +0100 Subject: [PATCH] module: fix crash when built-in module export a `default` key PR-URL: https://github.com/nodejs/node/pull/51481 Fixes: https://github.com/nodejs/node/issues/51480 Reviewed-By: Chengzhong Wu Reviewed-By: Guy Bedford Reviewed-By: Geoffrey Booth --- lib/internal/bootstrap/realm.js | 4 +++- test/parallel/test-process-default.js | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-process-default.js diff --git a/lib/internal/bootstrap/realm.js b/lib/internal/bootstrap/realm.js index 57ab47178d033d..f030f537a084d7 100644 --- a/lib/internal/bootstrap/realm.js +++ b/lib/internal/bootstrap/realm.js @@ -350,7 +350,9 @@ class BuiltinModule { const url = `node:${this.id}`; const builtin = this; const exportsKeys = ArrayPrototypeSlice(this.exportKeys); - ArrayPrototypePush(exportsKeys, 'default'); + if (!ArrayPrototypeIncludes(exportsKeys, 'default')) { + ArrayPrototypePush(exportsKeys, 'default'); + } this.module = new ModuleWrap( url, undefined, exportsKeys, function() { diff --git a/test/parallel/test-process-default.js b/test/parallel/test-process-default.js new file mode 100644 index 00000000000000..a6ceda2af3ee25 --- /dev/null +++ b/test/parallel/test-process-default.js @@ -0,0 +1,8 @@ +'use strict'; +const common = require('../common'); +const assert = require('node:assert'); + +process.default = 1; +import('node:process').then(common.mustCall((processModule) => { + assert.strictEqual(processModule.default.default, 1); +}));