What is npm? Basic introduction for beginners

access_time
hourglass_empty

What is NPM?

npm is the Node Package Manager that comes with and supports each development node of the Node. For many years, Node was programmers JavaScript Widely used for sharing tools, installing many different modules and managing dependency. Let's find out more about what npm is and how it works. It is very important to those who work with Node.js.

How does NPM work?

It works based on two roles:

  • It is a widely used repository for publishing open source Node.js projects. This is an online platform where people can publish and share stuff written in JavaScript.
  • npm is a command-line tool that helps interact with online platforms, like browsers and servers. This utility supports the installation and uninstallation of packages, version management and dependency management needed to run the project.

To use, you must install node.js - Because they are packed together.

What is node.js npm

The npm command line utility allows node.js to work correctly.

To use the package, your project must contain a file named package.json. In that package, you will find the project specific metadata.

Metadata shows a few things related to the project in the following order:

  • Name of project
  • Original version
  • Description
  • Entry point
  • Check out the commands
  • git respiratory
  • Key word
  • License
  • These dependencies
  • devDependencies

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

Here is an example of how you can define 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
  • Version is 1.0.0
  • This is npm guide for beginner
  • The main entry or file point of the project is beginner-npm.js
  • Keywords or tags to find projects in the repository are npm, example and basic
  • The author of the project is Hostinger International
  • This project is licensed under MIT
  • Other dependencies or modules that this module uses are express 4.16.4

How to install the npm module and start the project

First, you have to make sure node.js and npm installed. You can test by running a few simple commands.

To see if node.js is installed, open it Terminal or command line tool, and enter node -vThen the screen will display the version number if you have already installed it:

$ node -v
v0.10.9

To see if npm has been installed, enter npm -v. The screen will display the version number:

$ npm -v
1.2.25

If you have not already installed, you can get Node from node.js website and follow the prompts in the installer.

npm is famous for just installing with one line:

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

After installation, you can update npm because the new version is released regularly. To update, simply download the installer from the website node.js and run it again. The latest version will automatically replace the final version.

However, you can also update it using this command:

$ npm update -g npm

Initialize the project with npm

If you already have Node and npm, and you want to continue building the project, run the command npm init. This command will trigger to initialize the project.

For example, create a folder named test-npm and CD go there. Now let's run our first npm command:

$ npm init

This command functions as a tool to create the file package.json of the project. When you run the steps npm init, file package.json will be created and placed in the current directory.

Very helpful, the Node package manager’s init will explain 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)

Feedback of npm init

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

Click on "Enter ” to accept it. Later, npm init will give you a preview of package.json it's about to create.

The screen will display the result 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, save package.json. You can always change it later, either by editing the file directly or by playing it again npm init.

Install the npm module

The package in node.js contains all the files you need for a module. A module is a JavaScript library that you can put in a project.

Installing modules is one of the most basic things you should learn when starting with the Node package manager. Here is the command to install a module to the current directory:

$ npm install 
$ npm i

In the above command, replace by the name of the module you want to install.

For example, if you want to install Express - the most widely used and used web framework, you can 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 [email protected] No repository field.

+ [email protected]
added 48 packages from 36 contributors and audited 121 packages in 2.798s
found 0 vulnerabilities

mymac-MacBook-Pro: test npm mymac$

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

Whenever you install the module from npm, it will be installed to the directory node_modules.

This is the following display when you install the module in 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$ []

Epilogue

As you can see, apart from the main function is the online database for packages node.js differently, the goal of the Node package manager is to automatically manage the dependency for the file package.json with its Command Line Interface.

Here are the basic commands you should know:

$ curl https://npmjs.org/install.sh | sh
$ npm -v
$ npm init
$ npm install 

$ npm i

Now you understand what npm is and how it works basically. And if you're going to work with JavaScript, npm is an indispensable tool for your workflow.