access_time
hourglass_empty
person_outline

What Is npm? A Basic Introduction for Beginners

npm is the Node Package Manager that comes bundled with it and aids every Node development. For years, Node has been widely used by JavaScript developers to share tools, install various modules and manage their dependencies. Knowing this, it is critically important for people working with Node.js to understand what is npm.

How Does npm Work?

It works based on its two roles:

  • It is a widely used repository for the publishing of open-source Node.js projects. Meaning that is an online platform where anyone can publish and share tools written in JavaScript.
  • npm is a command line tool that helps to interact with online platforms, such as browsers and servers. This utility aids in installing and uninstalling packages, version management, and dependency management needed to run a project.

To use it, you have to install node.js – as they are bundled together.

node.js

The npm command line utility enables node.js to work properly.

In order to use packages, your project must contain a file called package.json. Inside that package, you’ll find metadata specific to the projects.

The metadata shows a few aspects of the project in the following order:

  • The project’s name
  • The initial version
  • Description
  • The entry point
  • Test commands
  • The git repository
  • Keywords
  • License
  • Dependencies
  • The devDependencies

Metadata helps to identify the project and acts as a baseline for users to get information about it.

Here is an example of how you can identify a project through its metadata:

{
  "name": "hostinger-npm",
  "version": "1.0.0",
  "description": "npm guide for beginner",
  "main": "beginner-npm.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "npm",
    "example",
    "basic"
  ],
  "author": "Hostinger International",
  "license": "MIT",
  "dependencies": {
    "express": "^4.16.4"
  }
}
  • The name is hostinger-npm
  • The version is 1.0.0
  • It is an npm guide for beginners
  • The project’s entry point or main file is beginner-npm.js
  • The keywords or tags to find the project in the repository are npm, example, and basic
  • The author of the project is Hostinger International
  • This project is licensed under MIT
  • The dependencies or other modules that this module uses are express 4.16.4

How to Install npm Modules and Start Projects?

First things first, you have to make sure that node.js and npm have been installed. Do so by running a few simple commands.

To see if node.js is installed, open the Terminal or a command line tool, and type node -v. This should show a version number if you already have it:

$ node -v 
v0.10.9

To see if npm is installed, type in npm -v. This should present the version number:

$ npm -v 
1.2.25

If you don’t have it installed, get Node from the node.js website, and follow the prompts in the installer.

npm is famous for its one-line installer:

$ curl https://npmjs.org/install.sh | sh

Once installed, you can update npm since new versions come out regularly. To update it, just download the installer from node.js site and run it again. The newest version will automatically replace your last one.

However, you can also update it by using this command:

$ npm update -g npm

Initializing A Project with npm

If you already have Node and npm, and you want to get on to building your project, run the npm init command. This will trigger the initialization of your project.

For example, let’s create a directory called test-npm and cd into it. Now let’s run our first npm command:

$ npm init

This command functions as a tool for creating a project’s package.json file. Once you run through the npm init steps, a package.json file will be generated and placed in the current directory.

Helpfully, Node package manager’s init explains what it does when you run it:

mymacs-MacBook-Pro: test-npm mymac$ npm init
This utility will walk you through creating a package.json file. 
It only covers the most common items, and tries to guess sane defaults. 

See `npm help json` for definitive documentation on these fields and exactly what they do. 

Use `npm install --save` afterwards to install a package and save it as a dependency in the package.json file. 

Press ^C at any time to quit. 
name: (test-npm)

Respond to npm init’s prompts.

name: (test-npm)hostinger-npm
version: (1.0.0)1.0.0
description: npm guide for beginner
entry point: (index.js) beginner-npm.js
test command:
git repository:
keywords: npm, example, beginner
author: Hostinger Internationallicense: (ISC) MIT

Press “Enter” to accept it. Then, npm init gives you a preview of the package.json it’s about to create.

It looks like this:

{
  "name": "hostinger-npm",
  "version": "1.0.0",
  "description": "npm guide for beginner",
  "main": "beginner-npm.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "npm",
    "example",
    "basic"
  ],
  "author": "Hostinger International",
  "license": "MIT",
  "dependencies": {
    "express": "^4.16.4"
  }
}

Is this OK? (yes) yes
mymac-MacBook-Pro: test-npm mymac$

Type “yes” and press “Enter” to confirm, saving the package.json. You can always change it later, either by editing the file directly or by running npm init again.

Installing npm Modules

A package in node.js contains all the files you need for a module. Modules are JavaScript libraries you can include in your project.

Installing modules is one of the most basic things you should learn to do when getting started with the Node package manager. Here’s the command to install a module into the current directory:

$ npm install <module> 

$ npm i <module>

In the command above, replace <module> with the name of the module you want to install.

For example, if you want to install Express – the most used and most well known node.js web framework, you could run the following command:

 $ npm install express
[mymac-MacBook-Pro:test-npm mymac$ npm install express]
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN hostinger-npm@1.0.0 No repository field.

+ express@4.16.4
added 48 packages from 36 contributors and audited 121 packages in 2.798s
found 0 vulnerabilities

mymac-MacBook-Pro: test npm mymac$

The command above will install the express module into /node_modules in the current directory.

Whenever you install a module from npm, it will be installed into the node_modules folder.

This is how it looks after you install a module in your project:

[mymac-MacBok-Pro: test-npm mymac$ cat package.json]
{
  "name": "hostinger-npm",
  "version": "1.0.0",
  "description": "npm guide for beginner",
  "main": "beginner-npm.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "npm",
    "example",
    "basic"
  ],
  "author": "Hostinger International",
  "license": "MIT",
  "dependencies": {
    "express": "^4.16.4"
  }
}
mymac-MacBok-Pro: test-npm mymac$ []

Conclusion

As you can see, besides its primary function as an online database for various node.js packages, the main goal of the Node package manager is an automated dependency and management for package.json files with its Command Line Interface.

These are basic commands you should know to:

  • Install npm:
$ curl https://npmjs.org/install.sh | sh
  • Check the version:
$ npm -v
  • Initialize a project:
$ npm init
  • Install modules:
$ npm install <module>

$ npm i <module>

If you’re planning to work with JavaScript, npm is an indispensable tool for your workflow.

 

The Author

Author

Fitrana A. / @fitrana

Fitrana is Hostinger's Digital Content Writer. She loves doing in-depth research to make every topic accessible to all readers. Besides writing, she enjoys attending music gigs, photo hunting, and reading books.

Related tutorials

Author

ahmadjito Reply

April 12, 2019

What an informative article, I've been looking for the clearest explanation about npm and I found it here. Thank you! I'm about to learn more about it.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!