You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
require.resolve should return package.json path if "main" in package.json is empty #32013
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:
index.js
package.json (missing main property)
and the path to index.js is now the require.resolve return value. So what about:
index.js
package.json (main property = "")
Right now the situation seems like a hack (using main: "")
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.
if the main property is missing or empty, then it should default to index.js?
That's the current (and documented) behavior. In fact, any "main" property that doesn't resolve to a file results in index.js being used.
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:
main: ""
no index.js file
require.resolve(x) => throws error
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:
Well... I don't want to say no outright but the problem with your {ifMainEmpty:true} suggestion is twofold:
the empty string is just a special case of the more general "main doesn't point to a file"
we've resisted extending require.resolve() because we want the module loader to be as fast and simple as possible
A resolvePackage() method might be acceptable. You're welcome to open a pull request but changes to the module loader get a lot of scrutiny so be prepared for that. :-)
Activity
test: use index.js if package.json "main" is empty
bnoordhuis commentedon Mar 2, 2020
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
"main"property that doesn't resolve to a file results inindex.jsbeing used.ORESoftware commentedon Mar 2, 2020
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:
bnoordhuis commentedon Mar 2, 2020
That breaks the correspondence between
require()andrequire.resolve():They should act identically but won't with your proposal.
bnoordhuis commentedon Mar 5, 2020
I'm going to close this as a wontfix per my previous comment but thanks anyway for the report.
ORESoftware commentedon Mar 5, 2020
maybe require.resolve with an options object?
or
require.resolvePackage()
bnoordhuis commentedon Mar 5, 2020
Well... I don't want to say no outright but the problem with your
{ifMainEmpty:true}suggestion is twofold:the empty string is just a special case of the more general "main doesn't point to a file"
we've resisted extending
require.resolve()because we want the module loader to be as fast and simple as possibleA
resolvePackage()method might be acceptable. You're welcome to open a pull request but changes to the module loader get a lot of scrutiny so be prepared for that. :-)test: use index.js if package.json "main" is empty
test: use index.js if package.json "main" is empty
test: use index.js if package.json "main" is empty
test: use index.js if package.json "main" is empty
test: use index.js if package.json "main" is empty