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

Check package.json is root of package and not {type: 'module'} #8

Open
overlookmotel opened this issue Feb 27, 2020 · 3 comments
Open

Comments

@overlookmotel
Copy link

With ESM modules, Node has introduced a 2nd use for package.json.

So now the presence of a package.json file does not necessarily indicate the root of a package. It could just contain {"type": "module"} to indicate that a subdirectory's files are to be treated as ESM modules.

Could pkg-dir check the contents of package.json files found, and ignore any which don't indicate package root? Perhaps checking for a name field would do the trick.

@sindresorhus
Copy link
Owner

Only packages on npm are required to have a name field, so I don’t think that’s a good heuristic. How about if the package.json only contains the type field?

@overlookmotel
Copy link
Author

Sorry for very slow reply. Life has been... strange.

In theory at least, package.json can be used for other purposes which don't indicate package root either.

e.g. a directory foo containing package.json reading {"main": "./bar.js"} makes require('./foo') resolve to ./foo/bar.js.

So I'm not sure what fields there are that can be reliably used to assess "rootyness".

Perhaps looking for either name, dependencies, devDependencies or peerDependencies?

If you don't think that's right either, then skipping over a package.json which contains only a type field (as you suggested) would at least deal with the most common case.

What do you think?

@rauschma
Copy link

IINM, property "name" is required in a top-level package.json – unless "private" is true. Thus, it could be detected by checking if either of these properties is present.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants