Where should the path from main in package.json point to?

Viewed 813

The package.json was created with the yarn cli. It fails when I try to build it. If I remove "main": "src/index.js" it works. src/index.js exists but it's an empty file for now.

Do I need to specify the path to my bundle or why does this throw an error? Changing the path to what is suggested in the error message doesn't work either.

package.json

{
  "name": "project",
  "version": "0.1.0",
  "description": "",
  "main": "src/index.js",
  "source": "src/index.html",
  "browserslist": "supports es6-module",
  "scripts": {
    "start": "parcel --open chrome",
    "build": "parcel build"
  },
  "repository": "",
  "author": "",
  "license": "MIT",
  "dependencies": {
    "eslint-config-airbnb": "^18.2.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.0"
  },
  "devDependencies": {
    "parcel": "^2.0.0-rc.0"
  }
}

Output

$ parcel build
 Build failed.

@parcel/namer-default: Target "main" declares an output file path of "src/index.js" which does not match the compiled bundle type "html".

  .../project/package.json:5:11
    4 |   "description": "project",
  > 5 |   "main": "src/index.js",
  >   |           ^^^^^^^^^^^^^^ Did you mean "src/index.html"?
    6 |   "source": "src/index.html",
    7 |   "scripts": {

   Try changing the file extension of "main" in package.json.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
1 Answers

The main field in package.json is intended to be used by library developers (e.g. if you are building a package that you intend to publish to npm to be consumed by others). In this context, it specifies the output target - i.e. where parcel should put an optimized commonjs javascript bundle. (see high-level documentation and detailed documentation).

In your case, it seems like you're building an app, not a library, so I would recommend just removing the main field entirely, which should resolve the errors you are seeing.

Related