Repository navigation
require.resolve should return package.json path if "main" in package.json is empty #32013
Description
Activity
- added a commit that references this issue
on Mar 2, 2020 - addedmoduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
on 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.
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 inindex.jsbeing 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:
require.resolve('@types/semver', {ifMainEmpty:""})- main:
default to the folder that contains package.json
That breaks the correspondence between
require()andrequire.resolve():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
{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. :-)-
- added a commit that references this issue
on Mar 9, 2020 - added a commit that references this issue
on Mar 9, 2020 - added 2 commits that reference this issue
on Mar 17, 2020 - added a commit that references this issue
on Mar 30, 2020
In NPM packages such as
@types/nodeand@types/semveretc, 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: "")