Introduction to npm and basic npm commands
npm stands for node package manager. It allows for seamless node.js package management. You can install, share and manage node.js packages.
npm consists of three components:
npm official website is https://www.npmjs.com/. Using this website you can find packages, view documentation, share and publish packages.
npm registry is a large database consisting of more than half a million packages. Developers download packages from the npm registry and publish their packages to the registry.
CLI(Command Line Interface):
The is the command line that helps in interacting with the npm for installing, updating and uninstalling packages and managing dependencies.
npm comes with the 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 meta data about the project.
First, head over to your project folder. You can create package.json from the command:
It asks you for some data like author name, description etc. You can just press enter for defaults.
To quickly create a package.json file. You can use command
npm init -y
To know more about the package.json see https://medium.com/beginners-guide-to-mobile-web-development/why-package-json-npm-basics-cab3e8cd150.
Basic npm commands:
- Locally: A locally installed package can be accessed only on the folder you’ve downloaded it.
There may be some warning during installation of the package. There is no need to worry about these warnings. After installing local package, your project folder looks like this
The node_modules is the folder in which our local packages are installed. There will be a new file named package-lock.json. This file contains the exact version of the package, unlike package.json which contains the semantic version(which we will be learning later).
You can also install packages as a developer dependency i.e., these packages are only needed for development. For example, they can be any package for testing the project. To install packages as a developer dependency use the command
npm install <package_name> --save-dev
- Globally: A globally installed packages works anywhere on the machine. To install global packages you’ve to use -g flag.
Generally, any packages you use in your project have to be installed locally. And packages you use in the command line are to be installed globally.
The command for the local and global packages are same except that you have to use -g flag for global packages.
Since we have installed packages sometimes we need to update our packages to get new features. To do that, you’ve to use
npm update <package_name>
for a specific package (or) just
to update all packages.
For global packages, you’ve to use -g.
npm update <package_name> -g
Sometimes you don’t need a particular package and you want to remove it. It’s not a good idea to manually remove the package from the node_modules folder as it can be a dependency for the other packages. So, to safely remove a package you’ve to use the command
npm uninstall <package_name>
For global packages,
npm uninstall <package_name> -g
Installing from package.json:
If you want to share your project then you may not want to share all your node modules. So, you will be sharing only your package.json which contains the packages needed for your project. And also If you want to contribute to some others project then you need to download the project and install packages in it. To do that, you have to use the command
This command will download all the packages the project needs.
List of installed packages:
To get the list of installed packages, use the command
This will list all the packages including its dependencies of all packages. The packages installed by you will be in the depth 0. Its dependencies will be in the depth 1 and further dependencies will be in the depth 2 and so on. To get packages of a certain depth, use the command
npm list depth <number>
All the package versions are represented with three digits. The first digit is major, second is minor and third is patch(see fig).
patch(~) is updated for bug fixes. You can update patch from the command
minor(^) is updated for every new functionality that doesn’t break the existing code.
major is updated for big changes. These generally break the existing code.
In the package.json, when you install a package, you will see a caret(^) symbol by default. This indicates that when a user is downloading your project, the package will be updated to the latest minor version. Same applies to patch. If we don’t include any symbol then exact version is downloaded. To get the latest major version, asterisk(*) is used. But you don’t want to do this as the major version can break your code.
To install either major, minor, patch (or) exact version, you can use the command
npm install <package_name>@x.y.z
Here you need to mention the x, y, z to install exact version, x and y to install the latest patch version, x to install the latest minor version and you can use normal install command to install the latest major version.
npm CLI has built -n help command. You can access it by
To get help for a particular command, use the command
npm <command> -h
You can also search npm documentation for help. To do that use
npm help-search <command>
Now you’ve learned all the basics of npm. To know more about npm you can go to the documentation in the official website(https://docs.npmjs.com/). Now you can start using it in your own projects