How to uninstall and update any dependencies through NPM ?
NPM (Node Package Manager) is the default package manager for Node.js. It manages all the packages and gets installed with the installation of Node.js. When we install any package into our project using npm client on the command line then it gets installed in the node_modules folder and the information such as the version of that package is reflected in the package.json file.
For Project Dependencies:
We can update the project dependencies using the update command:
We can update any particular project dependency using the following command:
npm update <packagename>
We can uninstall a project dependency using the following command:
npm uninstall <package_name>
For Global Dependencies:
We can update the global dependencies using the update command with the -g flag.
npm update -g
We can update any particular global dependency using the following command:
npm update -g <package_name>
We can uninstall a global dependency using the following command:
npm uninstall -g <package_name>
Step 1: Install Node.js if Node.js is not installed in your machine.
Step 2: Create a folder for your project and initialize a new Node.js project with default configurations using the following command on the command line.
npm init -y
Project structure: After following these steps your project folder should contain a package.json file.
Now to begin with the example we will install an old version of two packages named express and chalk using the following command on the command line.
npm install email@example.com firstname.lastname@example.org
To see the outdated packages for your project you can run the following command.
When using the outdated command we get the following output. In the output, the first column is the name of the package and the second column shows the version installed for our project. The third column represents the wanted version that is the version to which we can safely upgrade without any breaking changes. The fourth column represents the latest version of that package.
Now, when we use the npm update command then both the packages are updated to the latest safe versions and we get the following output. As the wanted and the latest version of the express were the same, it got updated to the latest version. The only outdated dependency we are left with is chalk.
Now when we uninstall the chalk package using uninstall command as shown below:
The content of the package.json file looks like the following: