Let's read together

What is NPM? Basic Definition for Beginners

What is npm

Maybe there are glimpses of questions in your mind like “What is npm?” when you become a developer. npm is the Node Package Manager. Over the years, Node is widely used by JavaScript developers to share tools, install various modules and manage their dependencies. Because of this, knowing npm is very important for people who work with Node.js.

How does npm work?

You need to know the two roles that npm is working on to gain insight into what npm is about and how it works. :

  • As a repository for publishing the open-source project Node.js. Which means, this platform becomes an offline container where anyone can publish and share tools written with JavaScript.
  • Npm is a command-line tool that can channel interactions on online platforms, for example browsers and servers. This utility can install and uninstall packages, manage the versions and dependencies needed to run the project.

To use it, you must install node.js – just as node.js and npm are in a bundle.

The main page of the package manager node

The npm command-line utility can make node.js work properly. To use the package, your project must have a file called package.json. In the package, you will find certain metadata in the project.

The metadata shows several aspects of the project in the following order:

  • Project name
  • Version
  • Description
  • Entry point
  • Test command
  • Git repository
  • Keyword
  • License
  • Dependency
  • dependency

Metadata can facilitate project identification and also act as a basis for users to obtain project information. Here is an example of how you can identify a project through its metadata:

{
  "name": "hosting-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"
  }
}
  • Named hosting-npm
  • Version is 1.0.0
  • An npm guide for beginners
  • The entry point of this project is beginner-npm.js
  • The keyword to find projects in the repository is npm, example, and basic
  • The creator of this project is Hostinger International
  • This project is licensed by MIT
  • The dependencies or other modules used are express 4.16.4

How to Install NPM Modules and Start a Project?

First, you must ensure that node.js and npm are installed. You can do this with a few simple commands.

To find out if node.js is installed open Terminal or the command-line tool and type node -v. This command will show the installed version of node.js like:

$ node -v 
v0.10.9

To find out if npm has also been installed, write npm -v. This command will show the npm version like:

$ npm -v 
1.2.25

If you don’t have Node, get the installer on the node.js website, and follow the installer’s instructions.

Npm is famous for its online installer:

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

Once installed, you can update npm because the latest version will always be updated regularly. To update, just download the installer from the node.js site and run it again. The latest version will replace the previous version automatically.

However, you can also edit it with this command:

$ npm update -g npm

How to Make a Project Using npm

If you already have Node and npm, then want to create a project, run the npm init command. This command will start your project.

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

$ npm init

This command as a tool to create a package.json file for the project is run. After you go through the npm init steps, a package.json file will be created and placed in the current directory.

The init command from the Node package manager explains the process that the command runs:

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)

Response to npm init prompt:

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 agree. Then, npm init will give you a preview of package.json that will be created.

The output will look 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 approve, save package.json. You can replace it later, either by editing the file directly or through command npm init again.

How to install npm Modules

The package on node.js contains all the files you need as a module. Modules are JavaScript libraries that you can put into projects.

Installing a module is the most basic thing to do when using the Node package manager. Following is the command to install the module to the current directory:

$ npm install <module> 

$ npm i <module>

In the command above, replace [ module ] with the name of the module that you want to install.

For example, when you want to install Express – web framework node.js, you can run the 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$

This will install the express module to / node_modules in the current directory.

When you install a module from npm, the module will be installed into the node_module folder.

Following is the output after you install the module into the 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 know, besides the main function of npm as an online database for various node.js packages, the main purpose of the Node package manager is automatic dependencies and management of the package.json file with its Command-Line Interfaces.

Here are the basic commands you need to know:

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

$ npm i <module>

Now, you already know what npm is and we guarantee, if you plan to work with JavaScript, npm can be a reliable tool.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More