Skip to content

module: createRequireFromPath documentation is incorrect #23710

@gillesdemey

Description

@gillesdemey
  • Version: v10.12.0
  • Platform: MacOS 10.14 (Darwin)
  • Subsystem:

The documentation for createRequireFromPath makes it seem like you can pass a directory to the function, but that will never correctly resolve packages.

Using the following directory structure;

src/
└── utils
    └── some-tool.js

The following code will throw a MODULE_NOT_FOUND exception (example from the docs):

const { createRequireFromPath } = require('module');
createRequireFromPath('../src/utils')('some-tool');

However, if we pass a file path we can resolve it just fine.

const { createRequireFromPath } = require('module');
createRequireFromPath('../src/utils/index.js')('some-tool');

Is there a bug here or is the documentation wrong? I'm willing to make a PR to either fix the issue or update the documentation :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.good first issueIssues that are suitable for first-time contributors.help wantedIssues that need assistance from volunteers or PRs that need help to proceed.moduleIssues and PRs related to the module subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions