Getting Started with npm

Rumman Ansari   Software Engineer   0000-00-00 00:00:00   30 Share
Subject Syllabus DetailsSubject Details 6 MCQ
☰ Table of Contents

Table of Content:


Getting Started with npm

npm , by default, comes with NodeJS.

Working with npm is simple. Type npm followed by the command.

To start with, find the version of npm by using the command "v" or "version".

>npm -v

You should now be able to see the version of npm.

Helpful npm

npm offers good help features. To view Help, type the following command.

>npm help

This will detail the usage of npm along with the list of commands available.

Using npm with "-h" on any specific command will provide the different ways in which a specific command can be used.

>npm -h search
Try it Out - npm help

Now try to get help on any of the npm commands such as "install".

You should be able to see various ways in which one can install a package or a specific version of a package from the npmjs repository or a git repository.

Hope you're getting the hang of it!

You realize that learning is fun only when you try something by yourself.

Searching for a Package

npm registry has over 500,000 packages. So, finding the required package can be daunting if you don't know the exact package name.

The different ways to search for the package are as follows:

  • Use the command "npm search" on your terminal.

  • Go to npmjs.com and find package.

  • Go to google.com and search for the package.

Now, search for redux thunk package using all these approaches.

Tips for Package Selection

While search results might return many packages, here are few points to consider while selecting a package:

  • Frequency of Updates

  • Availability of Documentation

  • Number of Downloads

  • Dependencies

  • Collaborators

For example, if you are looking for Google Maps API, It is better to avoid Google Static Maps API as this was published more than a year ago and is not updated.

Installing a Package Locally

If a package is required just for a specific project, you can install the package locally using:

>npm install <package name>

For example, running "npm install redux-thunk" will create a new folder "node_modules". Inside this, another folder "redux-thunk" will be created.

This folder will contain all Redux Thunk related files such as source code, license, readme file and package.json.

Installing a Package with Dependencies

Usually, packages contain references to a number of other packages. npm , by default, installs all the inner dependencies (packages) while installing a package.

For example, try to install "request".

Along with request, other dependencies like safe-buffer, safer-buffer also get installed.

Inside the "node_module/request" folder, navigate to the "node_module" folder created.

You will find that the folder contains files related to these packages.

Uninstalling Packages

You can uninstall packages using the command "npm uninstall <package name>"

Try to uninstall the packages redux and redux-thunk.

When the first package is uninstalled, that specific folder is deleted from the "node_modules" folder.

Once all packages are uninstalled, even the "node_modules" folder is automatically deleted from the project.

Topic Summary

From what you've seen so far, hope you are getting comfortable with using:

  • help to understand npm commands

  • search to find the required packages

  • installing and uninstalling packages

Make sure you spend time to try as you learn.

Now that you've understood the basics, let us move on to explore other core concepts and usage of npm commands.