-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
require.resolve should return package.json path if "main" in package.json is empty #32013
Comments
Verify that the module loader uses index.js when the "main" property of package.json is the empty string. Refs: nodejs#32013
I can't reproduce what you're describing. I've opened #32040 to add a regression test for this particular corner case but it passes without modifications to Node itself.
That's the current (and documented) behavior. In fact, any |
Right and it throws an error because index.js is not present, so my suggestion is to default to the folder that contains package.json? aka:
but instead of 3, I am hoping for it to return the path to package.json or the folder that contains package.json. To do this safely, we would need an options object like:
|
That breaks the correspondence between require('foo')
require(require.resolve('foo')) They should act identically but won't with your proposal. |
I'm going to close this as a wontfix per my previous comment but thanks anyway for the report. |
maybe require.resolve with an options object? or require.resolvePackage() |
Well... I don't want to say no outright but the problem with your
A |
Verify that the module loader uses index.js when the "main" property of package.json is the empty string. Refs: #32013 PR-URL: #32040 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Verify that the module loader uses index.js when the "main" property of package.json is the empty string. Refs: #32013 PR-URL: #32040 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Verify that the module loader uses index.js when the "main" property of package.json is the empty string. Refs: #32013 PR-URL: #32040 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Verify that the module loader uses index.js when the "main" property of package.json is the empty string. Refs: #32013 PR-URL: #32040 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Verify that the module loader uses index.js when the "main" property of package.json is the empty string. Refs: #32013 PR-URL: #32040 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
In NPM packages such as
@types/node
and@types/semver
etc, the "main" property in package.json is an empty string "".So when require.resolve() is called on those:
errors are thrown because the main property is empty..
so I am looking for a way to resolve the package location even if the main property is empty or missing..my suggestion would be to just return the location of package.json, something like:
I am not sure if the suggestion in the title is a good idea, but I am looking to find the location of package.json so I can read other properties from it.
One sane default - if the main property is missing or empty, then it should default to index.js?
Right now if it's missing, it should be something like:
and the path to index.js is now the require.resolve return value. So what about:
Right now the situation seems like a hack (using main: "")
The text was updated successfully, but these errors were encountered: