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

v5.2.0: require doesn't work in REPL #4208

Closed
shinnn opened this issue Dec 9, 2015 · 11 comments
Closed

v5.2.0: require doesn't work in REPL #4208

shinnn opened this issue Dec 9, 2015 · 11 comments
Labels
confirmed-bug Issues with confirmed bugs. module Issues and PRs related to the module subsystem. repl Issues and PRs related to the REPL subsystem.

Comments

@shinnn
Copy link
Contributor

shinnn commented Dec 9, 2015

Environment

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.1
BuildVersion:   15B42

$ node --version
v5.2.0

$ npm --version
3.3.12

$ pwd
/Users/shinnn/test

$ npm ls underscore
test@ /Users/shinnn/test
└── underscore@1.8.3

Problem

When I try to require locally installed "underscore" module, Node throws the following error.

$ node --version
v5.2.0
$ node
> require('underscore')
Error: Cannot find module 'underscore'
    at Function.Module._resolveFilename (module.js:327:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:355:17)
    at require (internal/module.js:13:17)
    at repl:1:1
    at REPLServer.defaultEval (repl.js:252:27)
    at bound (domain.js:281:14)
    at REPLServer.runBound [as eval] (domain.js:294:12)
    at REPLServer.<anonymous> (repl.js:417:12)
    at emitOne (events.js:83:20)

With Node v5.1.1 it works fine.

$ node --version
v5.1.1
$ node
> require('underscore')
{ [Function]
  _: [Circular],
  VERSION: '1.8.3',
  [...] }

Also, when I directly specify the script path instead of the simple package name underscore, it can load the module as expected.

$ node --version
v5.2.0
$ node
> require('./node_modules/underscore/underscore.js')
{ [Function]
  _: [Circular],
  VERSION: '1.8.3',
  [...] }
@ChALkeR ChALkeR added module Issues and PRs related to the module subsystem. repl Issues and PRs related to the REPL subsystem. labels Dec 9, 2015
@ChALkeR
Copy link
Member

ChALkeR commented Dec 9, 2015

Reproduced.

@ChALkeR
Copy link
Member

ChALkeR commented Dec 9, 2015

/cc @bnoordhuis and @cjihrig for ee72ee7, though I didn't bisect and I am not sure which commit broke that.

@silverwind silverwind added the confirmed-bug Issues with confirmed bugs. label Dec 9, 2015
@silverwind
Copy link
Contributor

Also confirmed here. I wonder how that got through our tests.

@MylesBorins
Copy link
Contributor

Doesn't appear to be a test that checks the repl loading a module atm.. trying to put together a test right now to make the bisect easier

@MylesBorins
Copy link
Contributor

edit: ignore original comment, test was throwing but it appears I didn't wire it up correctly

@silverwind
Copy link
Contributor

bisect confirms it's ee72ee7.

@bnoordhuis
Copy link
Member

Found the cause, the REPL's module object is missing the .paths property. Diff to illustrate what I mean:

diff --git a/lib/repl.js b/lib/repl.js
index 989e2e9..4d117e5 100644
--- a/lib/repl.js
+++ b/lib/repl.js
@@ -35,6 +35,7 @@ const Module = require('module');
 const domain = require('domain');
 const debug = util.debuglog('repl');

+const parentModule = module;
 const replMap = new WeakMap();

 try {
@@ -525,7 +526,8 @@ REPLServer.prototype.createContext = function() {
     context.global.global = context;
   }

-  const module = new Module('<repl>');
+  const module = new Module('<repl>', parentModule);
+  module.paths = parentModule.paths.slice(0);
   const require = internalModule.makeRequireFunction.call(module);
   context.module = module;
   context.require = require;

I'll put together a proper fix.

@silverwind
Copy link
Contributor

@bnoordhuis looking good, but I see a unnecessary lookup path repl/node_modules. Maybe it should be parentModule.paths.slice(1) to slice that off the lookup path array?

$ pwd
/Users/silverwind/git/test
$ NODE_DEBUG=module ../iojs/node
> require("underscore")
MODULE 1402: Module._load REQUEST underscore parent: <repl>
MODULE 1402: looking for "underscore" in ["/Users/silverwind/git/test/repl/node_modules","/Users/silverwind/git/test/node_modules","/Users/silverwind/git/node_modules","/Users/silverwind/node_modules","/Users/node_modules","/node_modules","/Users/silverwind/.node_modules","/Users/silverwind/.node_libraries","/Users/silverwind/git/iojs/out/lib/node"]
MODULE 1402: load "/Users/silverwind/git/test/node_modules/underscore/underscore.js" for module "/Users/silverwind/git/test/node_modules/underscore/underscore.js"

@bnoordhuis
Copy link
Member

#4215 - using Module._resolveLookupPaths().

bnoordhuis added a commit to bnoordhuis/io.js that referenced this issue Dec 9, 2015
Fix module loading of third-party modules in the REPL by inheriting
module.paths from the REPL's parent module.

Commit ee72ee7 ("module,repl: remove repl require() hack") introduced
a regression where require() of modules in node_modules directories
no longer worked in the REPL (and fortunately only in the REPL.)
It turns out we didn't have test coverage for that but we do now.

Fixes: nodejs#4208
PR-URL: nodejs#4215
Reviewed-By: Roman Reiss <me@silverwind.io>
bnoordhuis added a commit that referenced this issue Dec 15, 2015
Fix module loading of third-party modules in the REPL by inheriting
module.paths from the REPL's parent module.

Commit ee72ee7 ("module,repl: remove repl require() hack") introduced
a regression where require() of modules in node_modules directories
no longer worked in the REPL (and fortunately only in the REPL.)
It turns out we didn't have test coverage for that but we do now.

Fixes: #4208
PR-URL: #4215
Reviewed-By: Roman Reiss <me@silverwind.io>
@shinnn
Copy link
Contributor Author

shinnn commented Dec 18, 2015

Thanks for the fix and new release!

@silverwind
Copy link
Contributor

@shinnn we have to thank you for the quick report!

cjihrig pushed a commit to cjihrig/node that referenced this issue Jan 7, 2016
Fix module loading of third-party modules in the REPL by inheriting
module.paths from the REPL's parent module.

Commit ee72ee7 ("module,repl: remove repl require() hack") introduced
a regression where require() of modules in node_modules directories
no longer worked in the REPL (and fortunately only in the REPL.)
It turns out we didn't have test coverage for that but we do now.

Fixes: nodejs#4208
PR-URL: nodejs#4215
Reviewed-By: Roman Reiss <me@silverwind.io>
MylesBorins pushed a commit that referenced this issue Jan 19, 2016
Fix module loading of third-party modules in the REPL by inheriting
module.paths from the REPL's parent module.

Commit ee72ee7 ("module,repl: remove repl require() hack") introduced
a regression where require() of modules in node_modules directories
no longer worked in the REPL (and fortunately only in the REPL.)
It turns out we didn't have test coverage for that but we do now.

Fixes: #4208
PR-URL: #4215
Reviewed-By: Roman Reiss <me@silverwind.io>
scovetta pushed a commit to scovetta/node that referenced this issue Apr 2, 2016
Fix module loading of third-party modules in the REPL by inheriting
module.paths from the REPL's parent module.

Commit ee72ee7 ("module,repl: remove repl require() hack") introduced
a regression where require() of modules in node_modules directories
no longer worked in the REPL (and fortunately only in the REPL.)
It turns out we didn't have test coverage for that but we do now.

Fixes: nodejs#4208
PR-URL: nodejs#4215
Reviewed-By: Roman Reiss <me@silverwind.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. module Issues and PRs related to the module subsystem. repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

No branches or pull requests

5 participants