Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require.resolve() fails with an empty options object #28077

Closed
75lb opened this issue Jun 5, 2019 · 1 comment · Fixed by #28078
Closed

require.resolve() fails with an empty options object #28077

75lb opened this issue Jun 5, 2019 · 1 comment · Fixed by #28078

Comments

@75lb
Copy link

75lb commented Jun 5, 2019

  • Version: v12.4.0
  • Platform: macOS 10.14.5
  • Subsystem: module

In an empty directory, install lodash then from the REPL run the following commands. The second command fails if you pass an empty options object.

> require.resolve('lodash')
'/Users/lloyd/Documents/tmp/require-resolve/node_modules/lodash/lodash.js'

> require.resolve('lodash', {})
Thrown:
Error: Cannot find module 'lodash'
Require stack:
- <repl>
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:623:15)
    at Function.resolve (internal/modules/cjs/helpers.js:21:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '<repl>' ]
}

If the options value is {} or { paths: undefined } I would expect it to be ignored and the built-in defaults used.

This issue breaks a few of my apps in node >= 12.3.

@cjihrig
Copy link
Contributor

cjihrig commented Jun 5, 2019

This should be the fix:

diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index 5cb88da189..98cfe02a97 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -601,10 +601,11 @@ Module._resolveFilename = function(request, parent, isMain, options) {
     } else if (options.paths !== undefined) {
       throw new ERR_INVALID_OPT_VALUE('options.paths', options.paths);
     }
-  } else {
-    paths = Module._resolveLookupPaths(request, parent);
   }
 
+  if (paths === undefined)
+    paths = Module._resolveLookupPaths(request, parent);
+
   // Look up the filename first, since that's the cache key.
   const filename = Module._findPath(request, paths, isMain);
   if (!filename) {

PR coming.

cjihrig added a commit to cjihrig/node that referenced this issue Jun 11, 2019
If require.resolve() is passed an options object, but
the paths option is not present, then use the default
require.resolve() paths.

PR-URL: nodejs#28078
Fixes: nodejs#28077
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Yongsheng Zhang <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Anto Aravinth <[email protected]>
Reviewed-By: James M Snell <[email protected]>
BridgeAR pushed a commit that referenced this issue Jun 17, 2019
If require.resolve() is passed an options object, but
the paths option is not present, then use the default
require.resolve() paths.

PR-URL: #28078
Fixes: #28077
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Yongsheng Zhang <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Anto Aravinth <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants