NPM

Key Features of NPM (Node Package Manager)

  1. A Node.js package/module repository searchable on NPMSearcharrow-up-right

  2. A command-line utility for installing Node.js packages and managing versions/compatibility

Check npm installation

$ npm --version
6.13.4

npm version update

sudo npm install npm -g

Installing Modules using NPM

Syntax to install any Node.js module

npm install <Module Name>

Install a famous Node.js web framework module called express

Now you can use this module in your js file as following

Global vs Local Installation

By default, npm installs modules in local mode

  • Local mode

    : Means installing the package in the node_modules directory within the directory where the command was executed

  • Global mode

    : Means installing in the System directory

Install express in global mode

  • You can see that the module is installed in /usr/lib/node_modules, not in the current path

  • Since it saves to the system, you need to prefix with sudo if you are not the root user!

  • When installed in Global mode, you cannot directly require it in a node application

    • but, you can use the npm link command to import the module

package.json

: Located in the path of the Node application/module and defines the properties of the package

package.json generated when creating a project with express

  • package.json contains information about the modules and module versions that the project depends on

Uninstalling a Module

Updating a Module

Search a Module

  • This command consumes a lot of memory the first time it is used

    • If you are using a Cloud IDE or your server has about 1G of RAM, it will take a very long time or cause errors

    • In that case, search on NPMSearcharrow-up-right!

Last updated