GeezerVison is showing github.com — v5.2.0: `require` doesn't work in REPL · Issue #4208 · nodejs/node
v5.2.0: require doesn't work in REPL #4208Labels confirmed-bugIssues and PRs for confirmed bugs.moduleIssues and PRs related to the module subsystem.replIssues and PRs related to the REPL subsystem. DescriptionEnvironment
$ 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
└── [email protected]
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',
[...] }
Activityadded moduleIssues and PRs related to the module subsystem. replIssues and PRs related to the REPL subsystem. on Dec 9, 2015ChALkeR commented on Dec 9, 2015 ChALkeR commented on Dec 9, 2015 silverwind commented on Dec 9, 2015 Also confirmed here. I wonder how that got through our tests. MylesBorins commented on Dec 9, 2015 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 commented on Dec 9, 2015 edit: ignore original comment, test was throwing but it appears I didn't wire it up correctly silverwind commented on Dec 9, 2015 bnoordhuis commented on Dec 9, 2015 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 commented on Dec 9, 2015 @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 commented on Dec 9, 2015 #4215 - using Module._resolveLookupPaths(). shinnn commented on Dec 18, 2015 ContributorAuthor More actions Thanks for the fix and new release! silverwind commented on Dec 18, 2015 @shinnn we have to thank you for the quick report!
MetadataMetadataconfirmed-bugIssues and PRs for confirmed bugs.moduleIssues and PRs related to the module subsystem.replIssues and PRs related to the REPL subsystem. DevelopmentNo branches or pull requestsIssue actions
|