Skip to content

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

@ORESoftware

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

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