1

I installed Parcel with npm, everything installed fine without errors. I have a package.json like this:

{
    "name": "example",
    "version": "0.1.0",
    "source": "src/index.js",
    "main": "dist/index.js",
    "scripts": {
        "build": "parcel build"
    },
    "devDependencies": {
        "parcel": "^2.0.1"
    }
}

I'm just following the instructions on the website, but when I run npm run build it just says:

Debugger attached. Waiting for the debugger to disconnect...

Then it goes back to the command prompt. No files are touched or created. And actually parcel is not recognized as a command in the terminal. I tried uninstalling and re-installing Parcel but it still didn't work.

I did get some success with npx parcel build src/index.js, but the behaviour seemed weird to me. It looked like it installed Parcel all over again and left a .parcel-cache folder in my directory. It did produce an output js but all it did to the js was change the one global variable to something with a long string of numbers in the name. I just want to minify js. After npx runs that command the terminal freezes. Is that normal?

Moss
  • 3,695
  • 6
  • 40
  • 60
  • OK, I was having trouble installing Parcel, clearly, and I haven't gotten back to it. But the other major problem was that NPM config had ignore-scripts set to true, somehow. I became suspicious when everything I tried running via NPM did absolutely nothing. I discovered the solution here: https://stackoverflow.com/questions/59016328/npm-run-doesnt-do-anything – Moss Dec 02 '21 at 02:33

2 Answers2

2

It looks like you're having two problems - actually getting parcel installed correctly and getting unexpected output when you run it. For the first problem, I can't repro it with the config you provided, and I don't have any ideas beyond what Jos Faber already suggested in his answer.

For the second problem - when you run npx parcel, you are expecting minified output, but don't get it. That's because of the "source" and "main" fields in your package.json. These are intended to be used by library authors - i.e. people creating npm packages that get consumed by other projects rather than sent to the browser. You (typically) wouldn't want to minimize your distributed library code because it would interfere with the ability of people who used it to debug, tree-shake, and minimize their apps, so parcel doesn't do this by default. See parcel docs:

By default, optimization is enabled during production builds (parcel build), except for library targets [emphasis added].

If you're developing a web app, you'll want to remove "source" and "main" from package.json and change your cli command to parcel build src/index.js (parcel will put the output in a dist folder by default, but if you want to change that, use the --dist-dir cli flag).

On the off chance that you actually want to minify a library, you could do this by adding targets configuration to your package.json:

{
  ...
  "targets": {
    "main": {
      "optimize": true
    }
  }
}
Andrew Stegmaier
  • 3,429
  • 2
  • 14
  • 26
  • Would simply deleting the "main" line in the json have the same effect as "parcel build src/index.js"? (Can't test it yet, since parcel hasn't installed correctly.) – Moss Nov 16 '21 at 01:27
  • 1
    No - parcel needs some way of knowing where the root file of your app/library is - if you remove the "main" line from package.json (because you're not developing a library), you'll need to add a CLI parameter. – Andrew Stegmaier Nov 16 '21 at 15:37
  • 1
    Oh I think I misunderstood what you meant - you mean if you just leave the "source" field, can you omit the cli parameter? I think this will work. – Andrew Stegmaier Nov 17 '21 at 16:32
0

I've copied your example and everything runs as expected. You might want to remove the complete node_modules folder and npm i again.

Also, as a clean test, close your browsers. I'm not sure what your dev stack / environment is and there might be debuggers open in e.g. Chrome.

By the way, if parcel is not installed globally with the -g flag it's very possible the cli does not recognize the command. It's then only executable via script.

Jos
  • 1,387
  • 1
  • 13
  • 27