镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@ORESoftware

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

Activity

  1. added
    moduleIssues and PRs related to the module subsystem.
    on Mar 2, 2020
  2. bnoordhuis commented on Mar 2, 2020

    @bnoordhuis
    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.

  3. ORESoftware commented on Mar 2, 2020

    @ORESoftware
    ContributorAuthor

    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:""})
    
  4. bnoordhuis commented on Mar 2, 2020

    @bnoordhuis
    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.

  5. bnoordhuis commented on Mar 5, 2020

    @bnoordhuis
    Member

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

  6. ORESoftware commented on Mar 5, 2020

    @ORESoftware
    ContributorAuthor

    maybe require.resolve with an options object?

    or

    require.resolvePackage()

  7. bnoordhuis commented on Mar 5, 2020

    @bnoordhuis
    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. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    moduleIssues and PRs related to the module subsystem.wontfixIssues that will not be fixed.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions