'npm run build with parcel throws an error

Installed node, ran npm init to create package.json, installed parcel. running the server with npx parcel index.html runs the server. Then I changed "scripts" to "start": "parcel index.html" in package.json, and run npm run start, it also runs the server without a problem. And then I added to "scripts" "build": "parcel build index.html" and run npm run build. But this does not work... I get the error below...

> [email protected] build  
> parcel build index.html

× Build failed.

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

  C:\Users\ijevr\Desktop\JavaScript\vjezba 17\package.json:4:11
    3 |   "version": "1.0.0",
  > 4 |   "main": "index.js",
  >   |           ^^^^^^^^^^ Did you mean "index.html"?
    5 |   "scripts": {
    6 |     "start": "parcel index.html",

  ℹ Try changing the file extension of "main" in package.json.

Of course, changing the main to index.html states that the file should be a .js file... index.js is what the npm init created in the package.json. In my folder, my main js file is named script.js, and it was named like this before I ran npm init. However changing the main to script.js also does not help, I get the same error as stated here...

I don't know what to do to npm build it...



Solution 1:[1]

I didn't find a fix, but I found out that with Parcel 2 the build process changed. Works now.

Script should look like this:

"start": "parcel",
"build": "parcel build"

and main and source like this:

"main": "dist/index.js", //with dist being the path of the build

"source": "src/script.js", //and src the path of our project

Solution 2:[2]

Remove the main property from package.json completely, or add this to the package.json:

"targets": {
  "main": false
},

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Igor Jevremovic
Solution 2 Tyler2P