For enquiries call:

Phone

+1-469-442-0620

April flash sale-mobile

HomeBlogWeb DevelopmentHow to Uninstall NPM Package in Node [using `npm uninstall`]

How to Uninstall NPM Package in Node [using `npm uninstall`]

Published
05th Sep, 2023
Views
view count loader
Read it in
13 Mins
In this article
    How to Uninstall NPM Package in Node [using `npm uninstall`]

    Node JS took over the world by storm right after it was released in 2009. The main reason behind this is its package manager or NPM. This is mainly because it allows us to use different packages which sometimes lead to better performance of our app in the long run. This is done by either adding dependencies, dev-dependencies, etc. using npm. 

    It also provide us with different commands and parameters like install which is used to install any package for the project for doing various tasks which if done raw might take unnecessary time to complete, and its uninstall npm command which is just like the install command uninstalls packages that is not needed in the project. 

    Installing a package is a piece of cake. You just have to give the command with the specific flag and it’s done, but uninstalling a package is different. This is because, here, you have to take care of scopes which may vary depending upon the flag and the most important thing that is to be kept in mind is that there are several dependencies that get installed with the package into the project and needs to be taken care of while uninstalling or removing the package as it will unnecessary take space in the project. 

    In this blog, you will learn how to remove/delete npm package using the node uninstall command the right way while keeping different scopes in mind. But before that, you should have a thorough understanding of Node JS and you can achieve the expertise through Node.js Certification course on KnowledgeHut. 

    How to Uninstall NPM Packages Locally?

    As in the case of installing a package into our project’s root directory, we use 

    npm install package-name 

    Similarly, when uninstalling a package from the project’s root directory, you can simply replace the install word with uninstall in the above command. Thus, the command used for uninstalling npm packages is 

    npm uninstall <package-name> 

    When we use the above command, npm detects and removes that package from project’s node-modules directory,  package.json, npm-shrinkwrap.json, or package-lock.json file. This means that now you cannot use that package anymore in the project. 

    Let us understand this with the help of an example of Express JS - a Node JS Framework used for building single page, multipage and hybrid applications for both web and mobile. 

    Technically speaking, it is a layer above Node JS which is also written in JS and saves a lot of time by managing servers and routes but mostly by finding and identifying the exact part of the app where the bug is. Learn more about Node JS frameworks and libraries that just like Express JS, make the development process much easier and simpler with Full Stack Development course. 

    Below is the image of a package.json file of a project with Express JS already installed in it: 

    Let us see what happens if we run the npm uninstall command in a project’s CLI under root directory. 

    As it is quite clear from the above image that after running the uninstall command, Express JS Package which was installed in the root directory as a local package and can be seen in the package,json file under dependencies, cannot be seen as it has been completely uninstalled from the package. 

    Confirming Local Package Uninstallation

    The aforementioned sentence makes it pretty clear that the package will be uninstalled after using the uninstall command, but to be sure, you can simply check or search for that file's name within the package.json, npm-shrinkwrap.json, or package-lock.json file under dependencies heading as  depicted in the above image. 

    How to Uninstall NPM Package Globally?

    You can install and uninstall npm packages locally and globally. What it means is that you can install some packages directly in the machines in the C drive so that you don't have to re-install them time and again. 

    While locally means inside the project's root directory so that it can only be accessed within that project and not in any other project. NPM itself is the perfect example for globally installed package as you don't have to install it to install or uninstall other packages. 

    Just like you use the flag “-g” or “-global” before the name of the package to be installed in the packages globally, you use “-g” or “-global” before the name of the package you want to uninstall. 

    npm uninstall -g package-name 

    Or 

    npm uninstall -global package-name 

    For this, we will again be using the same example as we did in the case of locally uninstalling npm packages - Express JS. 

    Unlike local packages, global packages are not added as a dependency in the package.json file. Thus we have to run the following command to check and confirm that we have that package installed globally. 

    npm list - g 

    We can see in the above image that Express JS is listed as a globally installed package. Now, if we want to uninstall it, we simply have to run either of the following commands, and we are good to go. 

    npm uninstall --global express 

    Or 

    npm uninstall -g express 

    As we know, after running this uninstall command the npm package - Express JS with all its dependencies has been uninstalled globally which is quite clear from the below image Now if we want to use it, we again have to install it but this time we can do it either locally or globally depending upon our need.

    How to Uninstall Dev Dependencies Using NPM Uninstall?

    A dependency is a library that the project needs to work efficiently and effectively, while a dev dependency is a package that a developer needs during development but is not mandatory for the actual functionality of the project, examples include ESlint. 

    Just like every other NPM package, a separate flag is used to separate these dev dependencies at the time of installation and that flag is –D, this flag will also be used to delete or uninstall that package from the project. 

    npm uninstalll -D <package-name> 

    We will again be using the same example of Express JS but this time we have installed it as a dev dependency and not as a local or global package. Check the image below to get a better understanding.

    If we run the uninstall command with -D, it would be uninstalled completely with all of its other packages, on which it depends upon updating the package.json file as shown in the image below. 

    npm uninstall -D express 

    We can also use –save-dev flag for the same purpose of either installing or uninstalling dev dependencies but -D is more commonly used as it is easier to use and remember. 

    npm uninstalll -D <package-name> 

    or 

    npm uninstall --save-dev <package-name> 

    How to Remove Scoped and Unscoped Packages?

    Scoping is a way of combining related npm packages using a specified nomenclature. You just have to remember one thing : a scoped package always starts with ‘@’ while ‘/’ is used to separate packages. Eg - "@sample-scope/sample-package" 

    All the above methods provided above are for removing unscoped packages either globally or locally, but if we want to remove any scoped package locally or globally; 

    npm uninstall <@scope/package_name> //removing scoped packages locally 
    npm uninstall -g <@scope/package_name> // removing scoped packages globally 

    Additional Params and Options

    Until now we have seen many ways of uninstalling different packages and dependencies, but there are some other params and options which can also be used for the same purpose but give us an edge above the general method. 

    Flags

    At the beginning of this blog, we said that there are certain flags that can be used along with the package name at the time of either installing or uninstalling packages which specify its use as in how that package will be used in the project. 

    There are mainly 5 types of dependencies, and each has their unique role to play. Below are the flags used for each dependency which can be used while installing or uninstalling a package under that dependency name from the project.json file: 

    1. –save or -S 

    This flag is used when we have to either install or uninstall a package or dependency from a project completely with some other packages on which it was dependent and also want to remove its name from the project’s package.json, npm-shrinkwrap.json, or package-lock.json files. 

    npm uninstall -S <package-name> 
    npm uninstall --save <package-name> 
    1.  -no-save 

    This flag is especially used to uninstall a package from a project with all its dependencies but don’t want its name to be removed from the package.json file. 

    This flag comes in handy when you are forking a GitHub repo and want to remove a package that is not wanted or is not in use which was installed with every other package as per package.json but still want to keep that package name in the package.json file for future reference. 

    npm uninstall -no--save <package-name> 
    1. -O or –save-optional 

    This flag is used to either install or uninstall the optional dependencies. An optional dependency is a dependency that is not a must-have for the smooth functioning of the app but still is a dependency on which the project relies. 

    npm install --save-optional someDependency  

    Aliases

    Just like the flag, we have aliases for installing and uninstalling any npm package which again is true for all types of packages and dependencies. 

    1. un - Uninstalling an npm package or dependency 

     npm un <PACKAGE_NAME> 
    1. i - Installing npm package or dev dependency 

     npm i <PACKAGE_NAME> 
    1. rm -rf node_modules - this is used to delete the node modules folder from the JavaScript project recursively. 

     rm -rf node_modules/ 

    This should only be used if you are working on Mac or Linux OS as when used for windows will throw an error. So it is better to first install an npm package rimraf and then delete the node modules. If the version of npm is greater than 5.2, then there is no need to even install it. Just use the following command; 

    npm rimraf node_modules 

    What is NPM?

    NPM stands for Node Package Manager and as the name suggests it is used to manage packages that have either been published or will be published under Node Js. 

    What it means is that npm offers two types of services, the first and foremost is that it is an online repository for publishing open-source Node Js projects, and the second one, it provides a healthy interaction between the said repository which has already been published by aiding developers in package installation, version management, and dependency management. 

    Another thing that makes npm so handy is that you can install every package used in a specific project just by using the command npm install. It is especially used when you have forked any git repository and want to install every package that has been used in that project and is present in its package.json file in one go. 

    What Does NPM Uninstall Do?

    There are many ways of removing a package from the project's root directory so that we can’t use it: 

    1. Remove that file name from the package.json file or npm-shrinkwrap.json, or package-lock.json file. 
    2. Delete it from the project’s node modules so that you won't be able to use it. This is because the import statement is used to import different npm packages into the project from the node modules and if you delete it from node modules, you can’t use it. 

    But, the problem begins here, there are several dependencies that are installed with any npm package which are there for assistance in the project. Now, these files in the project’s node modules occupying space can’t be uninstalled as their names are not known. 

    This is where npm uninstall comes in, when we use the npm uninstall command, we are not only deleting the package but also its dependencies installed. At the same time, we have installed this package which in our case is Express JS and can be accessed from the previously mentioned method when we were just removing Express JS from node modules. 

    In the above image, we can see that after running the command for uninstalling Express JS, we have received a message stating - removed 57 packages. These are the packages that will still be there if we simply remove Express JS from node modules and will take up some space which might affect us in the long run. 

    These packages will not affect other packages or dependencies in any way as they are just dependencies on which Express depends to make our app efficient. 

    Looking to level up your coding skills? Learn Python online and unlock endless possibilities! From building websites to analyzing data, Python is the ultimate programming language. Start your journey today and become a Python pro in no time.

    Conclusion

    In this blog, we learned different ways in which we can quickly and effectively remove any type of package or dependency from our project. In general, we can uninstall any package or dependency using the npm uninstall command which goes like this 

    npm uninstall <package_name> 

    But there are many other ways in which we can do like this using different flags which give us additional control of installing and uninstalling packages for specific use cases like –save-dev is a flag used for installing packages (with the general install command) as a dev dependency which means that now that package will only be used by the developer during the development phase, and it can be uninstalled in the same way as it was installed by using this flag with the general uninstall command. 

    Well there are plenty more ways we can uninstall a package, and there are plenty more flags to use. Learn more about them through KnowledgeHut’s Node.js Certification course and become a master of NodeJS and NPM. 

    Frequently Asked Questions (FAQs)

    1How to Uninstall Node Modules?

    If we want to delete node modules from our project's root directory, there are two ways to do it. 

    1. rm -rf node_modules - this is used to delete node modules folder from our JavaScript project recursively but only from Mac and Linux OS. 

     rm -rf node_modules/ 
    1. For Windows, we have to first install an npm package rimraf and then delete the node modules. If the version of npm is greater than 5.2, then there is no need to even install it. Just use the following command. 

    npm rimraf node_modules 
    2How to Uninstall All Global Packages?

    There is no hard and fast rule or command which can delete all global packages in one go. We have to do it by writing the uninstall command with the –g flag followed by the names of all global packages. 

    npm uninstall -g <package name> <package name> <package name> 
    Profile

    Ateev Duggal

    Blog Author

    "I am Ateev Duggal, a front-end web developer, and a blogger. I write blogs mainly on React JS and have an experience of over 1.5 years of freelancing. I have worked on both static and dynamic projects again using React JS like a table to show clients data which was fetched using API, a review slider, pagination component, etc, and many websites like Parent, landing pages for Ataota, and many more.
    Some of my blogs have been published on freecodecamp, dev.to, Medium, Hashnode, and many other blogging platforms and developer's communities.
    My Primary skills not only include React JS but HTML5, CSS3, JS, Jquery, Git, and Bootstrap also. You can visit my GitHub Profile and LinkedIn profile for more information about me or you can visit my Website at tekolio.com".

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon