Install Node.js modules

Node.js comes with the npm package manager. Using it, you can install, uninstall and search for Node.js modules.

npm installs modules in two different scopes: local and global.

Local installation

The local installation is recommended, as the size of the packages is usually small. It installs the packages in a node_modules subdirectory of the current working directory, usually a project folder. This ensures all the required dependencies are installed and won’t break when installing other packages in a different project or locally.

To install modules locally, you just have to execute this command:

$ npm install PACKAGE

For example, to install MySQL, you would execute:

$ npm install mysql

Global installation

In other cases (typically when the module installs a command line tool), it is more convenient to install the packages globally. In these cases, the command varies slightly:

$ sudo npm -g install PACKAGE

For example, to install Express, you would execute:

$ sudo npm -g install express

This will make the Express command-line available in all projects.

We recommend installing packages in both local and global modes, so that you can share installed commands and still protect the dependencies of your project.

Last modification December 21, 2022