Repository navigation
Self referential modules not supported in repl or -r #31595
Description
Activity
- addedhelp wantedIssues that need assistance from volunteers or PRs that need help to proceed.Issues that need assistance from volunteers or PRs that need help to proceed.esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.
on Jan 31, 2020 - Note you need to add an “exports” main for self resolution to work.…On Fri, Jan 31, 2020 at 22:32 Myles Borins ***@***.***> wrote: If you attempt to require('package-name') using v13.7.0 in the repl it will fail with a 'MODULE_NOT_FOUND' error. // index.jsconsole.log('yay')// package.json { "name": "ohno", "main": "./index.js" } Uncaught Error: Cannot find module 'ohno' Require stack: - at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15) at Function.Module._load (internal/modules/cjs/loader.js:862:27) at Module.require (internal/modules/cjs/loader.js:1040:19) at require (internal/modules/cjs/helpers.js:72:18) { code: 'MODULE_NOT_FOUND', requireStack: [ '' ] } — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#31595?email_source=notifications&email_token=AAESFSTONW5SWBLGAB6G4UDRASDF3A5CNFSM4KONFOPKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IKH3U2Q>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAESFSUSMJ2YEDFLKWDQNVLRASDF3ANCNFSM4KONFOPA> .
I would like to try to help on this issue. I am trying a few things and for what I have seen so far in the commonjs loader, at this line the self resolution is skipped because the
parentmodule does not have afilenameproperty.@dnlup the
parent.filenamehere should correspond to aindex.jscontainingconsole.log(module.filename)giving the same filename as the parent in the place you've quoted. That is/path/to/index.jsis the parent which is used to determine the context for the self resolution.I just did a quick test of this with the following structure:
package.json
{ "name": "pkg", "exports": "./pkg.js" }index.js
require('pkg');
pkg.js
console.log('it works');
and can confirm this is working ok, so will close this issue.
If you are still finding any inconsistencies though please do let me know - the fact that you also ran into this means that there may be some documentation gaps that we need to do a better job of filling where the catch is happening here.
Sorry I misread this is specifically about calling from the repl.
In the case of the repl, strictly speaking can we consider the repl to be internal to a package I wonder?
@dnlup perhaps you can share the sort of use case you have in mind for using self resolution from the repl?
I could certainly get behind supporting this though - and yes it would be doing something like
module.id === '<repl>' ? process.cwd() + path.sep : module.filenameas the base.Thanks @guybedford for the quick answer.
it would be doing something like module.id === '' ? process.cwd() + path.sep : module.filename as the base
Sounds good.
In the case of the repl, strictly speaking can we consider the repl to be internal to a package I wonder?
I would think so, that is how I am making tests right now.
perhaps you can share the sort of use case you have in mind for using self resolution from the repl?
As of now, it is just something that I think it could be done to have more feature parity with
requireoutside the repl. Do not have a major use case, but I think it makes sense.- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.
on Mar 12, 2020 Is this done? Can I still help? :)
- added a commit that references this issue
on Jul 27, 2020 - added a commit that references this issue
on Sep 28, 2020 - added a commit that references this issue
on Oct 1, 2020 - added a commit that references this issue
on May 22, 2026
If you attempt to
require('package-name')using v13.7.0 in the repl it will fail with a 'MODULE_NOT_FOUND' error.