Last updated: Apr 4, 2024
Reading time·4 min
You can uninstall all npm packages by deleting the node_modules
folder and
the package-lock.json
file from your project's root directory.
Removing the node_modules
folder and the package-lock.json
files
uninstalls the local packages from your project.
You can delete your node_modules
folder and package-lock.json
file by
running the following commands on Windows.
# 👇️ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock
If you are on macOS or Linux, run the following commands instead.
# 👇️ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock
You can also clean your npm
cache after deleting node_modules
and
package-lock.json
.
npm cache clean --force
If you need to reinstall the dependencies of your project, issue the
npm install
command.
npm install
The command will install the dependencies
and devDependencies
packages from
your package.json
file.
Alternatively, you can use a command to uninstall all local packages.
Open your terminal in your project's root directory (where your package.json
)
file is and run the following command to uninstall all local packages.
npm uninstall `ls -1 node_modules | tr '/\n' ' '`
Here is a screenshot of running the command in Git Bash, on Windows.
The command will uninstall all local packages and will then remove them from
your package.json
file.
If you can't get the command to work, it's easier to just delete the
node_modules
folder and the package-lock.json
file as shown in the previous
subheading.
One way to uninstall all global npm packages is to use the npm ls -g
command
to print your globally installed packages and uninstall them.
npm ls -g --depth=0
The command lists all global top-level modules.
Note that npm
itself is installed as a global package. It is very likely that
you don't want to uninstall npm
itself because you might not be able to issue
npm
commands afterward.
Use the npm uninstall
command with the -g
flag to uninstall the globally
installed packages.
npm uninstall -g package1 package2 package3
If you are on macOS or Linux, issue the following command to uninstall all
globally installed packages (except for npm
).
npm ls -gp --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm
Here is a screenshot of running the command on Windows, in Git Bash.
The command uses npm ls -gp --depth=0
to list all global top-level packages.
The awk
portion of the command prints all modules other than npm
(because we
don't want to uninstall the global npm
module).
The xargs
portion of the command uninstalls the piped from awk
packages.
You can issue the npm ls -g
command to verify the global packages have been
uninstalled.
npm ls -g
If you are on Windows, open PowerShell and issue the following command to uninstall all globally installed packages.
npm -gp ls --depth=0 | ForEach-Object { Get-Item $_ } | Where { $_.Name -ne 'npm' } | ForEach-Object { npm rm -g $_.Name }
You can issue the npm ls -g
command to verify all globally installed packages
except for npm
have been uninstalled.
An alternative approach on Windows is to manually delete the globally installed
packages (except for npm
) from the C:\Users\<username>\AppData\Roaming\npm
directory.
You can open Explorer and enter %appdata%/npm
to navigate to the directory.
Simply type %appdata%/npm
in Explorer and press Enter
.
npm
and npx
folders.Select the packages you want to delete and delete them.
You can also delete the packages you want to uninstall from the node_modules
directory (except for npm
and npx
).
Alternatively, you can use the npm ls -g
command and uninstall all packages
based on its output.
npm ls -g --depth=0
The command lists all global top-level modules.
Note that npm
itself is installed as a global package. It is very likely that
you don't want to uninstall npm
itself because you might not be able to issue
npm
commands afterward.
Use the npm uninstall
command with the -g
flag to uninstall the globally
installed packages.
npm uninstall -g package1 package2 package3