Search code examples
package.json

Should I use both `main` and `exports` in a package.json?


The exports property (webpack docs, npm docs) of a package.json is relatively new feature, that allows to define multiple entry points for a package.

For example, we can provide a different entrypoint for require syntax vs import syntax, or we can allow imports like the-package/foo and the-package/bar.

Question is - given that my package has an exports property - is there any need to to have the main property? Perhaps for backwards compatibility for older tooling?


Solution

  • Yes, backwards compatibility for tooling is the main reason to include both "exports" & "main" properties.

    For example, here is an open Github issue for the eslint-plugin-import plugin whereby the resolver that eslint-plugin-import uses does not support the "exports" property and users will see lint warnings if the "main" property is not provided.

    If both "exports" & "main" properties are included, the "exports" property takes precedence for tooling that supports it, according to the npm docs that OP linked to, so there's no reason not to include both if backwards compatibility can be maintained.