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

module: createRequireFromPath missed docs #24763

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,11 @@ added: v10.12.0
function.
* Returns: {[`require`][]} Require function

Create a custom require function that will resolve modules relative to the filename path.

```js
const { createRequireFromPath } = require('module');
const requireUtil = createRequireFromPath('../src/utils');
const requireUtil = createRequireFromPath('../src/utils'); // can also be a file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I mistaken or won't the code fail if utils is a directory unless it's like this?:

Suggested change
const requireUtil = createRequireFromPath('../src/utils'); // can also be a file
const requireUtil = createRequireFromPath('../src/utils/.');

And if so, then the comment is misleading because it implies that passing a directory without /. at the end will work.

And explaining the /. requirement is probably more trouble than it's worth.

Probably best to update this to point to a file and not a directory at all.

Suggested change
const requireUtil = createRequireFromPath('../src/utils'); // can also be a file
const requireUtil = createRequireFromPath('../src/utils/index.js');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we should likely go with this last suggestion please.


// require `../src/utils/some-tool`
requireUtil('./some-tool');
Expand Down