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 documentation is incorrect #23710

Closed
gillesdemey opened this issue Oct 17, 2018 · 15 comments
Closed

module: createRequireFromPath documentation is incorrect #23710

gillesdemey opened this issue Oct 17, 2018 · 15 comments
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. help wanted Issues that need assistance from volunteers or PRs that need help to proceed. module Issues and PRs related to the module subsystem.

Comments

@gillesdemey
Copy link
Contributor

  • 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 :)

@demurgos
Copy link
Contributor

demurgos commented Oct 18, 2018

I don't believe the documentation is incorrect, but it could be improved.

Filename to be used to construct the relative require function.

Only a file can be a module. Your code is resolving from an extension-less file named utils in the src directory. You can't reliably use the path to differentiate directories from files (you can have a directory named utils.js, trailing separators are usually a source of bugs, etc.) and this function can be used with paths that do not exist on the FS.

It may be worth clarifying that a commonjs module id is the absolute system-dependent path to the corresponding file.

Edit: I missed the example below: I agree that it using a directory seems to be an error.

@targos
Copy link
Member

targos commented Oct 18, 2018

The example in the documentation seems indeed wrong because it passes a directory to the function.

/cc @devsnek

@guybedford
Copy link
Contributor

Yes it looks like the example should be changed to include a trailing separator:

const requireUtil = createRequireFromPath('../src/utils/');

@gillesdemey
Copy link
Contributor Author

gillesdemey commented Oct 18, 2018

@guybedford It works when appending a trailing slash and a ., not when just appending a trailing slash.

const requireUtil = createRequireFromPath('../src/utils/.');

@devsnek devsnek added doc Issues and PRs related to the documentations. module Issues and PRs related to the module subsystem. good first issue Issues that are suitable for first-time contributors. labels Oct 18, 2018
@guybedford
Copy link
Contributor

@gillesdemey thanks for confirming. That sounds incorrect to me. I specially included logic to handle retaining trailing slashes in the pathToFileURL function.

@guybedford
Copy link
Contributor

Ah, I get we're in path resolution and this is how Node path resolution works generally, which is a difference with how URLs behave.

@guybedford
Copy link
Contributor

It would seem sensible to me to specifically add support for a trailing separator in this function though.

@gillesdemey
Copy link
Contributor Author

I can submit a PR to support passing a directory?

The following change seems to do the trick :)

const searchPath = filename.endsWith('/') ?
    filename :
    path.dirname(filename);

  m.paths = Module._nodeModulePaths(searchPath);

@adeelibr
Copy link

Can I pretty please make a PR for this. This can be my first PR to NodeJS.

@guybedford
Copy link
Contributor

I'd be glad to see either PR along these lines for the docs or the trailing separator behaviour.

@gillesdemey
Copy link
Contributor Author

I've created a PR that will allow passing a directory, updating the documentation is still on my todo list 😄

@Trott Trott added the help wanted Issues that need assistance from volunteers or PRs that need help to proceed. label Nov 19, 2018
doertedev pushed a commit to doertedev/node that referenced this issue Nov 30, 2018
Fixes: nodejs#23710

Adding the missing documentation
@doertedev
Copy link

@gillesdemey 's that okay?

@creativecomposer
Copy link

Hi folks, stumbled upon this issue when searching for "good first issue" and "help wanted".

  1. There is a PR to allow passing a directory to createRequireFromPath that is still open.

  2. There is a doc PR that adds the comment // can also be a file to the example assuming that the aforementioned PR is landed.

If I understand correctly, the assumption made at (2) is incorrect.

Until the PR at (1) is accepted, the best option I believe is @Trott suggestion at #24763 (comment)

Shall I go ahead and make the doc change accordingly?

Just want to make sure my understanding is correct before creating a PR.

@guybedford
Copy link
Contributor

A docs PR to align the docs would be really great and is much needed, thank you @antonyj75 . I'd suggest pushing on the directory PR if you can as well - it could still likely land as-is I think.

@creativecomposer
Copy link

creativecomposer commented Mar 12, 2019

@guybedford thank you for your response.
Since doc alignment is much needed and is easy, I went ahead and created a PR for that.

The directory PR seem to be stuck while @gillesdemey added a negative test case.
I will need a bit more time to figure out what is going on there.

creativecomposer pushed a commit to creativecomposer/node that referenced this issue Mar 12, 2019
module.createRequireFromPath() takes a filename as argument.
But the accompanying code example in the documentation makes it seem
like it can take a directory argument as well.
However, if a directory is passed as argument instead of a filename,
then the function does not work as expected.

Therefore, the fix is to make explicit in the code example that
only filenames could be passed to module.createRequireFromPath()
and not directory names.

Fixes: nodejs#23710
Refs: nodejs#24763 (comment)
MylesBorins pushed a commit to gillesdemey/node that referenced this issue Apr 30, 2019
targos pushed a commit that referenced this issue May 4, 2019
Fixes: #23710

PR-URL: #23818
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Myles Borins <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. help wanted Issues that need assistance from volunteers or PRs that need help to proceed. module Issues and PRs related to the module subsystem.
Projects
None yet
9 participants