Skip to content

Self referential modules not supported in repl or -r #31595

Description

@MylesBorins

If you attempt to require('package-name') using v13.7.0 in the repl it will fail with a 'MODULE_NOT_FOUND' error.

// index.js
console.log('yay')
// package.json
{
  "name": "ohno",
  "main": "./index.js"
}
$ node
> require('ohno')

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: [ '' ]

}

$ node -r ./index.js
yay
$ node -r ohno

internal/modules/cjs/loader.js:983
throw err;
^

Error: Cannot find module 'ohno'
Require stack:

  • internal/preload
    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 Module._preloadModules (internal/modules/cjs/loader.js:1296:12)
    at loadPreloadModules (internal/bootstrap/pre_execution.js:435:5)
    at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:68:3)
    at internal/main/repl.js:18:1 {
    code: 'MODULE_NOT_FOUND',
    requireStack: [ 'internal/preload' ]
    }

Activity

  1. added
    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.
    esmIssues and PRs related to the ECMAScript Modules implementation.
    on Jan 31, 2020
  2. guybedford commented on Feb 1, 2020

    @guybedford
    Contributor
  3. dnlup commented on Mar 6, 2020

    @dnlup
    Contributor

    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 parent module does not have a filename property.

  4. guybedford commented on Mar 6, 2020

    @guybedford
    Contributor

    @dnlup the parent.filename here should correspond to a index.js containing console.log(module.filename) giving the same filename as the parent in the place you've quoted. That is /path/to/index.js is 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.

  5. guybedford commented on Mar 6, 2020

    @guybedford
    Contributor

    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.filename as the base.

  6. dnlup commented on Mar 6, 2020

    @dnlup
    Contributor

    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 require outside the repl. Do not have a major use case, but I think it makes sense.

  7. almosnow commented on Jun 2, 2020

    @almosnow

    Is this done? Can I still help? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.feature requestIssues requesting new Node.js features.help wantedIssues that need assistance from volunteers or PRs that need help to proceed.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions