As commonly known, any npm module can be installed by running a simple command: npm install <module_name>
.
I have installed a few modules that I do not use any more and I just want to get them off. I have a few questions regarding this:
Do we have any command or process to uninstall a module from the root (something like npm uninstall <module_name>
)
or will simply removing the module files do?
How does it affect us if we keep the unused modules?
I found this out the hard way, even if it is seemingly obvious.
I initially tried to loop through the node_modules directory running npm uninstall module-name
with a simple for loop in a script. I found out it will not work if you call the full path, e.g.,
npm uninstall module-name
was working, but
npm uninstall /full/path/to/node_modules/module-name
was not working.
Well, to give a complete answer to this question, there are two methods (for example we call the installed module as module1):
To remove module1 without changing package.json:
npm uninstall module1
To remove module1 with changing package.json, and removing it from the dependencies in package.json:
npm uninstall --save module1
Note: to simplify the above mentioned commands, you can use -S instead of --save , and can use remove, rm, r, un, unlink instead of uninstall
To uninstall the Node.js module:
npm uninstall <module_name>
This will remove the module from folder node_modules, but not from file package.json. So when we do npm install again it will download the module.
So to remove the module from file package.json, use:
npm uninstall <module_name> --save
This also deletes the dependency from file package.json.
And if you want to uninstall any globally module you can use:
npm -g uninstall <module_name> --save
This will delete the dependency globally.
If you want to uninstall a number of modules, then just run the npm uninstall
.
Then go to file package.json and delete the unwanted module from there, and then just run the command npm install
. It should fix your problem.
If it doesn't work with npm uninstall <module_name>
try it globally by typing -g
.
Maybe you just need to do it as an superUser/administrator with sudo npm uninstall <module_name>
.
Additionally, if you've started using yarn
, in place of npm
:
yarn remove <package-name>
Is the equivalent of:
npm uninstall <package-name> --save
This will
- remove the package from package.json
, as well as
- uninstall it from your project's node-modules
folder
In case you are on Windows, run CMD as administrator and type:
npm -g uninstall <package name>
Sometimes npm uninstall -g packageName
doesn’t work.
In this case you can delete package manually.
On Mac, go to folder /usr/local/lib/node_modules
and delete the folder with the package you want. That's it. Check your list of globally installed packages with this command:
npm list -g --depth=0
To uninstall a module using npm, you can use:
npm uninstall moduleName
Also, if you want to uninstall and want the change to be reflected in your package.json then you can use the --save flag, like this:
npm uninstall moduleName --save
OR
npm uninstall -S
And if you want to uninstall a module from devDependencies and want the change to be reflected in package.json then you can use -D flag, like this:
npm uninstall moduleName -D
To remove packages in folder node_modules in bulk, you could also remove them from file package.json, save it, and then run npm prune
in the terminal.
This will remove those packages, which exist in the file system, but are not used/declared in file package.json.
P.S.: This is particularly useful on Windows, as you may often encounter problems with being unable to delete some files due to the "exceeded path length limit".
For Windows users - if you want to remove all the Node.js modules installed at once:
cd node_modules
)It will uninstall all the modules.
In npm v6+
npm uninstall <package_name>
removes it both in folder node_modules and file package.json.
Use
npm uninstall <package_name>
Example to uninstall express
npm uninstall express
The simplest solution is:
npm uninstall packageName --save-dev
See upper level packages names in the your project:
npm list --depth=0
The output will be like:
[email protected] /home/jackkobec/projects/myAppName
+-- packageName@packageVersion
+-- [email protected]
Copy package name and execute npm uninstall command. Example for express package:
npm uninstall express --save-dev
You can also run the following as shorthand:
npm un packageName
or npm rm packageName
Note: Add -g
at end of command to uninstall global packages.
# Log in as root (might be required depending on install)
su -
# List all global packages
npm ls -g --depth=0
# List all local (project) packages
npm ls -p --depth=0
# Remove all global packages
npm ls -g --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm
# Remove all local packges
npm ls -p --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -p rm
# NOTE (optional): to use node with sudo you can add the bins to /usr/bin
# NOTE $PATHTONODEINSTALL is where node is installed (e.g. /usr/local/node)
sudo ln -s $PATHTONODEINSTALL/bin/node /usr/bin/node
sudo ln -s $PATHTONODEINSTALL/bin/npm /usr/bin/npm
You can delete a Node.js module manually. For Windows,
Go to the node_modules
directory of your repository.
Delete the Node.js module you don't want.
Don't forget to remove the reference to the module in your package.json file! Your project may still run with the reference, but you may get an error. You also don't want to leave unused references in your package.json file that can cause confusion later.
Update for npm 5:
As of npm 5.0.0, installed/uninstalled modules are added/removed as a dependency by default, so the --save
option is no longer needed.
Run
npm uninstall <package>
For example:
npm uninstall mongodb
It will remove the module from the node_modules folder and also the package.json file.
I just install stylus by default under my home dir, so I just use npm uninstall stylus
to detach it, or you can try npm rm <package_name>
out.
The uninstall
option didn't work for me when I tried to use the same command to the one I used in installing (as I was installing with the @latest
directive)
So for example, I installed a package like this:
npm install @ngtools/webpack@latest
And then I wanted to uninstall it, so I used the same command (including @latest):
npm uninstall @ngtools/webpack@latest
So the above uninstall didn't work. I have to remove the @latest
, and then it worked well:
npm uninstall @ngtools/webpack
To uninstall the Node.js module:
npm uninstall <module_name>
This will remove the module from folder node_modules, but not from file package.json.
Remove the module from package.json use by using this command:
npm uninstall <module_name> --save
This also deletes it from package.json.
Source: Stackoverflow.com