Categories
Uncategorized

Add Node Packages Globally

When you install a new nodejs package, it will get installed in your home directory by default. This is usally /home/$USER where $USER is ‘centos’ on a Centos machine . In order to run a module GLOBALLY on your machine, you will need to install the package globally and set the NODE_PATH environment variable after the installation.

For example, to install ‘puppeteer’ globally run this command:

sudo npm install -g --save puppeteer --unsafe-perm=true --

This will install the ‘puppeteer’ package to /usr/lib/node_modules/puppeteer/

However, if you try to run a puppeteer script from anywhere on your server, you will get an error saying:

internal/modules/cjs/loader.js:895
throw err;  ^
Error: Cannot find module 'puppeteer'

To make puppeteer available globally on your machine simply add the path to the node_modules to your server’s environment variables like so:

For linux machines, you will need to add the line given (for each distro) to your bash profile. Do ‘sudo vi ~/.bash_profile ‘ ; add the line given below; save the file; then logout and login to the SSH environment for the changes to take effect:

  • Centos: export NODE_PATH=/usr/lib/node_modules
  • Ubuntu: export NODE_PATH=/usr/local/lib/node_modules
  • Windows 7/8*: Add to path C:\Users\{USERNAME}\AppData\Roaming\npm\node_modules
  • Windows 10*: Add to path %AppData%\npm\node_modules


*On windows system, you can add to your path by following these steps:

  • Open the Start Search, type in “env”, and choose “Edit the system environment variables”
  • Click the “Environment Variables…” button.
  • Under the “System Variables” section (the lower half), find the row with “Path” in the first column, and click edit.
  • The “Edit environment variable” UI will appear. Here, you can click “New” and type in the new path you want to add. From this screen you can also edit or reorder them.
  • Dismiss all of the dialogs by choosing “OK”. Your changes are saved!
  • Restart the machine (or at least the apps that require the path) in order for the changes to take effect.

 1,451 total views,  1 views today