From 2223dc06fd4f5a1988ee18844f877f38b18ce15d Mon Sep 17 00:00:00 2001 From: David Gobaud <240811+dgobaud@users.noreply.github.com> Date: Wed, 7 Aug 2019 19:05:22 -0700 Subject: [PATCH 1/2] fix crash when using from cli require.main is undefined when running from cli - don't use unless it exists --- index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 7551a0f..2ea21a3 100644 --- a/index.js +++ b/index.js @@ -82,7 +82,9 @@ function addPath (path) { if (modulePaths.indexOf(path) === -1) { modulePaths.push(path) // Enable the search path for the current top-level module - addPathHelper(path, require.main.paths) + if (require.main) { + addPathHelper(path, require.main.paths) + } parent = module.parent // Also modify the paths of the module that was used to load the @@ -115,7 +117,9 @@ function addAlias (alias, target) { function reset () { // Reset all changes in paths caused by addPath function modulePaths.forEach(function (path) { - removePathHelper(path, require.main.paths) + if (require.main) { + removePathHelper(path, require.main.paths) + } // Delete from require.cache if the module has been required before. // This is required for node >= 11 From b37745065d36dbd013334be0656de5dc35d6e25d Mon Sep 17 00:00:00 2001 From: Daniel Garnier-Moiroux Date: Mon, 30 Sep 2019 08:50:25 +0200 Subject: [PATCH 2/2] Add tests for REPL mode - This require introducing a way of having "require.main" undefined in our module. The require function is different between the test module and the module under test, so we can't just do `require.main = null` in the test function, as it won't affect the "require" function of the prod code. So we need to introduce a helper function for evaluating require.main, and injact a flag in the test module when running the tests. --- index.js | 19 +++++++++++++------ test/specs.js | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 2ea21a3..8f2e570 100644 --- a/index.js +++ b/index.js @@ -82,14 +82,15 @@ function addPath (path) { if (modulePaths.indexOf(path) === -1) { modulePaths.push(path) // Enable the search path for the current top-level module - if (require.main) { - addPathHelper(path, require.main.paths) + var mainModule = getMainModule() + if (mainModule) { + addPathHelper(path, mainModule.paths) } parent = module.parent // Also modify the paths of the module that was used to load the // app-module-paths module and all of it's parents - while (parent && parent !== require.main) { + while (parent && parent !== mainModule) { addPathHelper(path, parent.paths) parent = parent.parent } @@ -115,10 +116,12 @@ function addAlias (alias, target) { * The function is undocumented and for testing purposes only */ function reset () { + var mainModule = getMainModule() + // Reset all changes in paths caused by addPath function modulePaths.forEach(function (path) { - if (require.main) { - removePathHelper(path, require.main.paths) + if (mainModule) { + removePathHelper(path, mainModule.paths) } // Delete from require.cache if the module has been required before. @@ -130,7 +133,7 @@ function reset () { }) var parent = module.parent - while (parent && parent !== require.main) { + while (parent && parent !== mainModule) { removePathHelper(path, parent.paths) parent = parent.parent } @@ -209,6 +212,10 @@ function init (options) { } } +function getMainModule () { + return require.main._simulateRepl ? undefined : require.main +} + module.exports = init module.exports.addPath = addPath module.exports.addAlias = addAlias diff --git a/test/specs.js b/test/specs.js index 0582e79..4793827 100644 --- a/test/specs.js +++ b/test/specs.js @@ -158,6 +158,24 @@ describe('module-alias', function () { }) }) + context('when used from the REPL', function () { + before(function () { + require.main._simulateRepl = true + }) + + after(function () { + delete require.main._simulateRepl + }) + + it('should addPath', function () { + moduleAlias.addPath('some-path') + }) + + it('should reset', function () { + moduleAlias.reset() + }) + }) + it('should support forked modules', function () { expect(typeof require('hello-world-classic')).to.equal('function') })