Skip to content

NPM

NPM (Node Package Manager) is a package manager for Node.js with hundreds of thousands of packages.

The main goal is to automate dependency and package management. This means you can specify all project dependencies in the package.json file, and then any time you need to start working with the project, you can runnpm install and install all dependencies immediately.

What is Node.js?

Node.js allows you to write applications in JavaScript on the backend (server) side. It is based on the V8 runtime and written in C ++.

It was originally intended to be a server environment for applications, but developers started using it to create tools to help automate local tasks. Since then, a whole new ecosystem of Node.js based tools has evolved to change the face of the front-end.

Installing Node.js

To install Node.js, go to the official website of the project and select the version appropriate for your system. Windows and Mac installers are available, as well as precompiled binaries for Linux.

On Linux, you can also install Node through the package manager.

Installation via pre-installed installers is usually limited to 2 steps:

  1. Download the LTS installer from the official Node.js website.
  2. Follow the instructions in the installer * (do not install additional tools) *.

You can check the effectiveness of the installation using the command line:

node -v
npm -v

Both commands should return the installed software version. If it doesn't, it's a good idea to restart your computer.

The package.json file

As a general rule, any project using Node.js will need a package.json file.

The file package.json can be described as a project manifest that contains the packages and modules it depends on, information about its unique control source, as well as specific metadata such as project name, description, and author.

Inside package.json, you will almost always find project-specific metadata stored as JSON - whether it's a web application, a Node.js module, or even a regular JavaScript library. This metadata helps identify the project and provides a point of reference for users and colleagues to obtain information about the project.

Another important aspect of this file is that it contains a set of dependencies for a given project. These dependencies are the modules on which the project relies to function properly. It also allows you to separate dependencies needed for production dependencies from dependencies needed for project development (devDependencies). In production, you probably won't need a tool to compile your CSS and JS files when they change. But in both production and development, you will want modules such as framework, libraries, and other tools.

Sample package.json file:

{
  "name": "my-app", // Project name
  "version": "0.1.2", // Project version
  "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
  "license": "MIT", // Project licence
  "devDependencies": {
    "@angular/animations": "~10.0.6",
    "@angular/common": "~10.0.6",
    "@angular/compiler": "~10.0.6",
  },
  "dependencies": {
    "@angular-devkit/build-angular": "~0.1000.5",
    "@angular/cli": "~10.0.5",
    "@angular/compiler-cli": "~10.0.6",
  }
}

Generating the package.json file

The command npm init is a tool to generate thepackage.json file for a project. It will ask you to enter some data about the project:

  • Project name
  • Initial version of the project
  • Project description
  • Project entry point (main project file)
  • Project testing command
  • Project git repository
  • Project Keywords (tags related to the project)
  • Project license

If you are satisfied with the suggestion that the npm init command makes next to the prompt, you can simply hitEnter to accept the suggestion and move on to the next property.

If you don't want to waste time answering these questions, you can use the --yes flag in thenpm init command to automatically populate all options with their default values.

After completing the necessary data, the package.json file will be generated and placed in the current directory.

Installation of individual modules

Installing modules is one of the most basic things you can do with npm.

To install a standalone module to the current directory:

npm install <module name>

The above command will install the <module name> module in the / node_modules directory in the current folder.

Installation of all modules

In addition to running the installation of a single module, you can run the installation of all modules that are listed as dependencies and devDependencies in the packages.json file in the project directory. To do this, just run the install command:

npm install

It is worth adding that the install command has a shorter alias of the letteri itself.

Save module as project dependencies

When executing the command npm install, you can add the optional --save flag to install the module. This flag will add the module as a production dependency to the project in the package.json file.

npm install <module name> --save

Save the module as a development dependency

A similar flag, --save-dev, will also save the module as a dependency to the project, but only as a module to be used during project development, i.e. as a module that is needed to build the application but does not need to be used in production.

npm install <module name> --save-dev

Global module installation

To install the module globally, use the --global flag when running theinstall command. Such modules will be available throughout the system, not just inside the project folder.

npm install <module name> --global

The alias for this flag is the abbreviated --g flag.

Global modules can be extremely useful - there are many helpful tools you can install globally.