-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Comments
I don't believe the documentation is incorrect, but it could be improved.
Only a file can be a module. Your code is resolving from an extension-less file named 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. |
The example in the documentation seems indeed wrong because it passes a directory to the function. /cc @devsnek |
Yes it looks like the example should be changed to include a trailing separator: const requireUtil = createRequireFromPath('../src/utils/'); |
@guybedford It works when appending a trailing slash and a const requireUtil = createRequireFromPath('../src/utils/.'); |
@gillesdemey thanks for confirming. That sounds incorrect to me. I specially included logic to handle retaining trailing slashes in the pathToFileURL function. |
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. |
It would seem sensible to me to specifically add support for a trailing separator in this function though. |
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); |
Can I pretty please make a PR for this. This can be my first PR to NodeJS. |
I'd be glad to see either PR along these lines for the docs or the trailing separator behaviour. |
I've created a PR that will allow passing a directory, updating the documentation is still on my todo list 😄 |
Fixes: nodejs#23710 Adding the missing documentation
@gillesdemey 's that okay? |
Hi folks, stumbled upon this issue when searching for "good first issue" and "help wanted".
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. |
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. |
@guybedford thank you for your response. The directory PR seem to be stuck while @gillesdemey added a negative test case. |
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)
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]>
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;
The following code will throw a
MODULE_NOT_FOUND
exception (example from the docs):However, if we pass a file path we can resolve it just fine.
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 :)
The text was updated successfully, but these errors were encountered: