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

package.json should include an explicit "main" entry #1009

Open
mstange opened this issue May 26, 2021 · 7 comments
Open

package.json should include an explicit "main" entry #1009

mstange opened this issue May 26, 2021 · 7 comments

Comments

@mstange
Copy link
Contributor

mstange commented May 26, 2021

This was previously discussed in #94: It's good practice to include a "main": "main-file.js" entry in package.json, so that the generated npm package is usable without specifying a file name.

The current workaround is to use wasm-pack build --out-name index, or to manually adjust package.json after the build. However, even with the main file being called "index.js", it would be better to have an explicit "main": "index.js" entry, according to this comment by @ljharb, who is on the nodejs Modules team.

@ljharb
Copy link

ljharb commented May 26, 2021

As long as you're making changes, it'd be also good to add:

"exports": {
  ".": "./index.js",
  "./package.json": "./package.json"
},

since adding that field is usually a breaking change, so it's good to add it earlier

@DoctorBracewell
Copy link

Running into this exact issue - one of the build tools I'm using to bundle the output package uses node's require.resolve(packageName) function which complains that it can't find the package if the package.json doesn't contain a main field.

I might take a look into submitting a PR that adds an explicit "main": "file-name.js" field to the package.json - can anyone think if that would be a breaking change or have any unintended side-effects?

@DoctorBracewell
Copy link

DoctorBracewell commented Jun 17, 2022

Taking a look at the package.json generation code and output for each build target it seems like the nodejs target is the only one that includes a main field, other targets such as bundler or no-modules use properties such as module or browser, which according to this SO answer are not officially supported by node (but still recognised by a number of different bunders/package managers etc.)

Given Ijharb's comment, I'd assume it best to just include a main property for the package.json regardless of the build target? Which shouldn't be too hard because the value that main should have exists in some form for each target output anyway.

@ljharb
Copy link

ljharb commented Jun 18, 2022

Yes, always include "main" as CJS; there's no advantage to omitting it.

@DogLooksGood
Copy link

Hey, any news. I saw the PR is blocked?
The main property is kinda necessary for using with bundlers like Vite.

@ronsims2
Copy link

ronsims2 commented May 1, 2023

@DogLooksGood, I ran into the same problem and I wrote a couple of scripts to work around the missing main by adding it: https://gist.github.com/ronsims2/78ed52583f84056969577286b881c0bc

@DogLooksGood
Copy link

@ronsims2 Yeah, we are using a patch to add a "main" entry

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

5 participants