[BUG] npm update --global fails: npm ERR! global requires an add or rm option
Problem
Current Behavior: [code block] Expected Behavior: Before v7.0.0, running `npm update --global` would update all the packages installed globally which are outdated. Steps To Reproduce: Install an outdated global package, and try to update all the packages. Environment: - OS: Ubuntu 20.04.1 - Node: 12.19.0 - npm: 7.0.0
Error Output
Error: global requires an add or rm option
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix npm update --global command for version 7.0.0
In npm version 7.0.0, the behavior of the 'npm update --global' command changed, requiring explicit add or rm options for global package management. This was a significant shift from previous versions where 'npm update --global' would update all outdated global packages without additional parameters.
Awaiting Verification
Be the first to verify this fix
- 1
Check for outdated global packages
Before updating, check which global packages are outdated by running the command. This will help you identify what needs to be updated.
bashnpm outdated --global --depth=0 - 2
Update specific global packages
Since 'npm update --global' is not functioning as expected, you can update each outdated package individually using the following command format.
bashnpm install --global <package-name> - 3
Create a script to automate updates
To streamline the process of updating all global packages, create a shell script that runs the update command for each outdated package identified in step 1.
bashfor pkg in $(npm outdated --global --parseable | awk -F'/' '{print $NF}'); do npm install --global $pkg; done - 4
Consider downgrading npm
If you prefer the previous behavior, consider downgrading npm to a version prior to 7.0.0 where 'npm update --global' worked as expected.
bashnpm install -g npm@6
Validation
Confirm the fix by running 'npm outdated --global --depth=0' again to ensure no outdated packages remain. Additionally, verify that the packages have been updated by checking their versions with 'npm list --global --depth=0'.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep