From 8829b0debc567b31332f85f1b413f029f3abf483 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 5 Jan 2023 16:12:18 +0100 Subject: [PATCH] module: fix unintended mutation Refs: https://github.com/nodejs/node/pull/46061#issuecomment-1372246648 --- lib/internal/modules/cjs/loader.js | 10 +++++----- test/fixtures/require-resolve.js | 5 +++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 092e9545b43563..9fb2f1b72a5e39 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -29,6 +29,7 @@ const { ArrayPrototypeJoin, ArrayPrototypeMap, ArrayPrototypePush, + ArrayPrototypePushApply, ArrayPrototypeSlice, ArrayPrototypeSplice, ArrayPrototypeUnshift, @@ -668,12 +669,11 @@ Module._findPath = function(request, paths, isMain) { return filename; } - if (exts === undefined) { - exts = ['']; - } else { - ArrayPrototypeUnshift(exts, ''); + const extensions = ['']; + if (exts !== undefined) { + ArrayPrototypePushApply(extensions, exts); } - reportModuleNotFoundToWatchMode(basePath, exts); + reportModuleNotFoundToWatchMode(basePath, extensions); } return false; diff --git a/test/fixtures/require-resolve.js b/test/fixtures/require-resolve.js index 86bc6415c8eb55..9a7fc3385143aa 100644 --- a/test/fixtures/require-resolve.js +++ b/test/fixtures/require-resolve.js @@ -98,3 +98,8 @@ assert.strictEqual( require.resolve('./printA.js', {}), require.resolve('./printA.js') ); + +assert.strictEqual( + require.resolve('no_index/'), + path.join(__dirname, 'node_modules', 'no_index', 'lib', 'index.js'), +)