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

require.resolve should return package.json path if "main" in package.json is empty #32013

Closed
ORESoftware opened this issue Feb 28, 2020 · 6 comments
Labels
module Issues and PRs related to the module subsystem. wontfix Issues that will not be fixed.

Comments

@ORESoftware
Copy link
Contributor

ORESoftware commented Feb 28, 2020

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:

require.resolve('@types/node') 
require.resolve('@types/semver')

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:

require.resolve('@types/semver', {ifMainEmpty:""})

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

bnoordhuis added a commit to bnoordhuis/io.js that referenced this issue Mar 2, 2020
Verify that the module loader uses index.js when the "main" property of
package.json is the empty string.

Refs: nodejs#32013
@bnoordhuis bnoordhuis added the module Issues and PRs related to the module subsystem. label Mar 2, 2020
@bnoordhuis
Copy link
Member

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.

@ORESoftware
Copy link
Contributor Author

ORESoftware commented 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:

  1. main: ""
  2. no index.js file
  3. 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:""})

@bnoordhuis
Copy link
Member

default to the folder that contains package.json

That breaks the correspondence between require() and require.resolve():

require('foo')
require(require.resolve('foo'))

They should act identically but won't with your proposal.

@bnoordhuis bnoordhuis added the wontfix Issues that will not be fixed. label Mar 5, 2020
@bnoordhuis
Copy link
Member

I'm going to close this as a wontfix per my previous comment but thanks anyway for the report.

@ORESoftware
Copy link
Contributor Author

maybe require.resolve with an options object?

or

require.resolvePackage()

@bnoordhuis
Copy link
Member

Well... I don't want to say no outright but the problem with your {ifMainEmpty:true} suggestion is twofold:

  1. the empty string is just a special case of the more general "main doesn't point to a file"

  2. 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. :-)

addaleax pushed a commit that referenced this issue Mar 9, 2020
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>
MylesBorins pushed a commit that referenced this issue Mar 9, 2020
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>
codebytere pushed a commit that referenced this issue Mar 17, 2020
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>
codebytere pushed a commit that referenced this issue Mar 23, 2020
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>
codebytere pushed a commit that referenced this issue Mar 30, 2020
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module Issues and PRs related to the module subsystem. wontfix Issues that will not be fixed.
Projects
None yet
Development

No branches or pull requests

2 participants