NodeJS require a global module/package

npm link
nodejs modules
nodejs using global modules
nodejs global
where global node modules
nodejs require path
nodejs export class
nodejs global variable

I'm trying to install globally and then use forever and forever-monitor like this:

npm install -g forever forever-monitor

I see the usual output and also the operations that copy the files to the global path, but then if I try to require("forever"); I get an error saying that the module wasn't found.

I'm using latest version of both node and npm and I already know about the change that npm made in global vs local install, but I really don't want to install localy on every project and I'm working on a platform that doesn't support link so npm link after a global install isn't possible for me.

My question is: why I can't require a globally installed package? Is that a feature or a bug? Or am I doing something wrong?

PS: Just to make it crystal clear: I don't want to install locally.

In Node.js, require doesn't look in the folder where global modules are installed.

You can fix this by setting the NODE_PATH environment variable. In Linux this will be:

export NODE_PATH=/usr/lib/node_modules

Note: This depend on where your global modules are actually installed.

See: Loading from the global folders.

NodeJS require a global module/package, NodeJS require a global module/package. I'm trying to install globally and then use forever and forever-monitor like this: npm install -g forever forever-monitor. This script demo-global.js will be the one that we'll expose globally with a custom name on our package.json. 3. Expose your module for global installation. In this case, we want our global module to have the name dosomethingawesome, npm makes it pretty easy, to register our global module we need to specify the bin property to our package.json. Note that you can do as many commands you want, a lot of packages have one or more executable files that they'd like to install into the PATH.

After you install package globally you have to link the local project with global package

npm install express -g
cd ~/mynodeproject/
npm link express  

See here

Requiring modules in Node.js: Everything you need to know, The require module, which appears to be available on the global scope — no "​start.js" }' > node_modules/find-me/package.json ~/learn-node  When you first install a package to a Node.js project, npm automatically creates the node_modules folder to store the modules needed for your project and the package-lock.json file that you examined earlier. Confirm these are in your working directory. In your shell, type ls and press ENTER. You will observe the following output:

Apologies for the necromancy but I'm able to specify hard-coded paths to globally installed modules:

var pg = require("/usr/local/lib/node_modules/pg");

This isn't perfect but considering that Unity3d tries to "compile" all javascript that is included in the project directory I really can't install any packages.

Where does npm install packages?, is that it reads a JavaScript file, executes the file, and then proceeds to return the exports object. Modules are a crucial concept to understand Node.js projects. In this post, we cover Node modules: require, exports and, the future import. Node modules allow you to write reusable code. You can nest them one inside another. Using the Node Package Manager (NPM), you can publish your modules and make them available to the community.

I know this is an old question, but I ran into this when trying to do some version checking using semver in a preinstall script in package.json. Since I knew I can't depend on any local modules installed, I used this to require semver from the global node_modules folder (as npm depends on it I know it's there):

function requireGlobal(packageName) {
  var childProcess = require('child_process');
  var path = require('path');
  var fs = require('fs');

  var globalNodeModules = childProcess.execSync('npm root -g').toString().trim();
  var packageDir = path.join(globalNodeModules, packageName);
  if (!fs.existsSync(packageDir))
    packageDir = path.join(globalNodeModules, 'npm/node_modules', packageName); //find package required by old npm

  if (!fs.existsSync(packageDir))
    throw new Error('Cannot find global module \'' + packageName + '\'');

  var packageMeta = JSON.parse(fs.readFileSync(path.join(packageDir, 'package.json')).toString());
  var main = path.join(packageDir, packageMeta.main);

  return require(main);

I like this approach because this doesn't require the install of any special modules in order to use.

I didn't go with a NODE_PATH solution like others have suggested since I wanted to get this to work on anyone's machine, without having to require additional configuration/setup before running npm install for my project.

The way this is coded, it is only guaranteed to find top-level modules (installed using npm install -g ...) or modules required by npm (listed as dependencies here: If you are using a newer version of NPM, it may find dependencies of other globally installed packages since there is a flatter structure for node_modules folders now.

Hope this is useful to someone.

What is require?, in the node_modules directory. It is not concerned at all with the "dependencies" section of the package. json. freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Where does Node.js look for modules?, as JavaScript libraries - a certain part of your overall codebase (for example, a collection of functions) which you want to keep together, but which you also want to keep separated from the rest of your codebase to keep things cleanly separated. A module is any file or directory in the node_modules directory that can be loaded by the Node.js require () function. To be loaded by the Node.js require () function, a module must be one of the following: A folder with a package.json file containing a "main" field. A folder with an index.js file in it.

Explained: What are Node.js modules?, Accessing the main module; Addenda: Package Manager Tips; Addenda: The node_modules Folders; Loading from the global folders; The module wrapper. Go inside npm folder and check if node_module folder exist, if not create a new folder named as node_modules. You all global modules will come in this folder after completing all steps. Final Step: Go to CMD and right the following command: npm set prefix C:\Users\username(Number)\AppData\Roaming\npm\node_modules

Modules, In Node.js, require doesn't look in the folder where global modules are installed. You can fix this by setting the NODE_PATH environment variable. In Linux this  Node Packaged Modules. npm can install packages in local or global mode. In local mode, it installs the package in a node_modules folder in your parent working directory. This location is owned by

NodeJS require a global module/package - node.js - html, The directory used by npm for globally installed npm modules. const globalModules = require('global-modules');. Whether to install a package globally or locally depends on the global config, which is aliased to the -g command line switch. Just like how global variables are kind of gross, but also necessary in some cases, global packages are important, but best avoided if not needed. In general, the rule of thumb is:

  • I just leave it here
  • Possible duplicate of How do I import global modules in Node? I get "Error: Cannot find module <module>"?
  • so it's ~/.config/yarn/global for yarn
  • On my Ubuntu 13.10 machine, the global path for modules is different than you show here. I had to use export NODE_PATH=/usr/local/lib/node_modules instead.
  • If you're on Windows 7 / 8 and haven't overridden any of Node's install defaults, setting the NODE_PATH environment variable to C:\Users\{USERNAME}\AppData\Roaming\npm\node_modules will likely work.
  • @WesJohnson Just %AppData%\npm\node_modules will work on Windows 10.
  • If I set NODE_PATH can I use global and local modules simultaneously?
  • Alternatively instead of a static path, i.e. if you are using NVM: NODE_PATH=$(npm root -g)
  • I'm running on a platform that doesn't support link (as my question states)
  • which platform are you using ?
  • I really don't want to mess with link (nor symbolic links at all). I just want to install packages globally and require them. I know NPM was re-designed to avoid this, but how hard could it be to achieve something like this?
  • What if I don't have a project? Say ~/some-stand-alone-random-nodejs-test.js. I don't want to turn my home folder into a project directory. I don't want to create new folders for every small experiment.
  • Worked perfect on Windows 8.1. From node command line cd to my projects' local node_modules folder then executed npm link <module> Then you will see a shortcut(link) created in your projects' node_module folder referencing the global node module.
  • Unity3D doesn't support JavaScript. It supports a JS-like syntax for its Boo interpreter/compiler (Boo is a Python-like language for .NET) that is deceptively marketed as "JavaScript". The more-accurate name for the language Unity supports is UnityScript. Because it's not even close to the same language, next to none of the JS written for the web or for Node.js will work in Unity. Much more info on the differences on the official Unity wiki: