2.10.2. Global installation of Node.js modules

Attention!

Whenever you change Linux configuration, the ~/.bashrc and ~/.bash_profile files are overwritten and all manual changes to them must be added again.

Notes:

  • In commands, instead of node22 you can specify the name of any directory where you want to place modules.
  • After editing ~/.bash_profile to apply changes in the console, use the . ~/.bashrc command or simply reconnect via SSH.

Node.js modules can be stored either in each project's directory or in a common directory that is available to all projects in a given hosting account. Global installation can be convenient when there are multiple projects and they use the same modules.

By default, executing npm with the -g flag (or –global) attempts to install packages in the Node.js system directory of the form /usr/local/node22/bin/node/bin|lib, to which access is restricted. The installation directory can be changed to a different directory.

  1. Create a directory for modules (if not created):
    mkdir ~/node22
  2. Override the default directory:
    npm config set prefix '~/node22'
  3. To make modules available to apps, add the following line to the end of the ~/.bashrc file:
    export PATH=~/node22/bin:$PATH
  4. (Optional) To make modules available in the console, add the same line to the end of the ~/.bash_profile file.

After that, modules can be installed from any directory using npm i -g module_name and will be available to all apps in a given hosting account. Installed modules will be placed in the ~/node22 directory.

Content