npm / cli

the package manager for JavaScript
https://docs.npmjs.com/cli/
Other
8.47k stars 3.16k forks source link

[BUG] `npm update -g ds` removes all global packages and npm itself #7731

Open eAlexandrohin opened 2 months ago

eAlexandrohin commented 2 months ago

Is there an existing issue for this?

This issue exists in the latest npm version

Current Behavior

Title. Basically self-destructs and purges all global packages and you can't even run npm after. As I have tested, it works with any package that is not installed, even non-existent ones.

npm update -g [non-installed package]

To fix it, you need to re-install whole Node again with nvm.

Expected Behavior

Don't self-destruct and delete all global packages, when you specify package that is not installed.

Steps To Reproduce

  1. run npm update -g ds
  2. get the result
    removed 203 packages in 641ms
    The batch file cannot be found.
  3. try to run npm again npm -v
    'npm' is not recognized as an internal or external command,
    operable program or batch file.
  4. become aware of your unimportance to the universe

Environment

ljharb commented 2 months ago

npm update should never be run with -g.

eAlexandrohin commented 2 months ago

npm update should never be run with -g.

but how you should update your global packages then?

ljharb commented 2 months ago

One at a time, manually, with npm install. (also you probably shouldn't have any global packages)

rsxdalv commented 3 weeks ago

Here's the log I saw, hopefully it helps others find this issue:

PS C:...> npm i -g tsc
npm ERR! code EEXIST
npm ERR! path C:\Users\admin\AppData\Roaming\npm\tsc
npm ERR! File exists: C:\Users\admin\AppData\Roaming\npm\tsc
npm ERR! with --force to overwrite files recklessly.

npm ERR!     C:\Users\admin\AppData\Local\npm-cache\_logs\2024-10-14T15_28_08_921Z-debug.log
PS C:...> npm update -g tsc
removed 2019 packages, and audited 1 package in 8s

PS C:...> npm update -g tsc
npm error Class extends value undefined is not a constructor or null

For me, fixing it required uninstalling nvm before reinstalling node the proper way (perhaps reinstalling node through nvm would work too).