In this blog we will see how to install npm, and master common commands in the Node Package Manager (npm) features and command-line interface. So, let’s start!
Node Package Manager:
Here’s a list of the most common commands you’ll use when working with npm:
npm is installed with Node.js. So, you don’t have to install it separately. You can install node.js from their official website https://nodejs.org/en/download/. After installing node, You can check the version of node and npm by
The package.json is the project manifest file. Using package.json you can manage dependencies and write scripts. It has all the metadata about the project.
For creating package.json firstly, move to your project folder and run this command:
It asks you for some data like author name, description etc. You can just press enter for defaults.
Install npm Updates
If you realize you have an older version, you can update as follows:
- Locally: A locally installed package can be accessed only on the folder you’ve downloaded it. To perform a local install all you have to do is:
I have used here the flag –save so the package name and version is saved into your package.json file.
- Globally: You can also install packages globally, which means Node.js will be able to access them from any project you might need. To install global packages you’ve to use the -g flag.
Since we have installed packages sometimes we need to update our packages to get new features. For updating particular package run this:
Or run this to update all packages:
And for updating global packages, you’ve to use -g flag:
Let’s assume that my current package is causing us compatibility problems. So, We can remove the package and install an older version:
For global packages,
List of installed packages:
This will list all the packages including its dependencies of all packages. Run this command:
Check the registry if any specified package is outdated. It prints a list of all packages which are outdated.
Runs a command that is defined in the start property in the scripts. If not defined it will run the node server.js command.
It is used to build a package.
Searches the npm registry for packages matching the search terms.
Manages ownership of published packages. It is used to manage package owners. You can list it by:
We can add owner by:
You can also remove owner by:
Searches npm help documentation for a specific topic. It is used whenever the user needs help to get some reference.
This has been an overview of the npm commands and features that I find most useful. Now you’ve learned all the basics of npm.You can start using it in your own projects. Thanks for being with me till the end. If you find this blog helpful do share with your friends.