Introduction to npm and basic npm commands

C Dharmateja
Mar 16, 2018 · 5 min read
Image for post
Image for post

Introduction:

npm consists of three components:

  1. Website
  2. Registry
  3. CLI

Website:

npm official website is https://www.npmjs.com/. Using this website you can find packages, view documentation, share and publish packages.

Registry:

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.

Installing npm:

After installing node, You can check the version of node and npm by

node -v
npm -v

package.json:

creating package.json:

First, head over to your project folder. You can create package.json from the command:

npm init

It asks you for some data like author name, description etc. You can just press enter for defaults.

Image for post
Image for post

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:

Installing packages:

Image for post
Image for post

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

Image for post
Image for post

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
  1. Globally: A globally installed packages works anywhere on the machine. To install global packages you’ve to use -g flag.
Image for post
Image for post

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.

Updating packages:

npm update <package_name>

for a specific package (or) just

npm update

to update all packages.

Image for post
Image for post

For global packages, you’ve to use -g.

npm update <package_name> -g

Uninstalling packages:

npm uninstall <package_name>
Image for post
Image for post

For global packages,

npm uninstall <package_name> -g

Installing from package.json:

npm install

This command will download all the packages the project needs.

Image for post
Image for post

List of installed packages:

npm list

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>
Image for post
Image for post

Semantic versioning:

Image for post
Image for post
semantic versioning

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.

Getting help:

npm help

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>

Conclusion:

Beginner's Guide to Mobile Web Development

Tutorials to guide beginners to perform various tasks in…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store