Require and resolve global modules in node.js like a boss.
Differences with require()
requireg tries to find modules in global locations which are
not natively supported by the node.js module resolve algorithm.
It support both npm/yarn global packages installation paths.
- $HOME/node_modules (instead of $HOME/.node_modules)
- $HOME/node_libraries (instead of $HOME/.node_libraries)
- $HOME/node_packages (specific of
- $PREFIX/lib/node_modules (instead of $PREFIX/lib/node)
- $NODE_MODULES (use the specific modules path environment variable)
- Resolve via native
require()(unless second parameter is true)
- User home directory (
- Node installation path
- $NODE_MODULES (can have different multiple paths, semicolon separated)
- Common operative system installation paths
$ npm install requireg --save[-dev]
$ yarn add requireg
requireg(path: string, onlyGlobal: boolean = false)
Load global modules
var requireg =// require a globally installed packagevar npm =
Load only global modules
var requireg =// require a globally installed package and skip local packagesvar eslint =
Resolve module path
var modulePath = requireg// returns '/usr/local/lib/node_modules/npm/lib/npm.js'
Now it is globally available from any source file
var globalModule =
Module not found
requireg maintains the same behavior as the native
It will throw an
Error exception if the module was not found
- Require global modules in node.js is considered anti-pattern.
Note that you can experiment unreliability or inconsistency across different environments.
I hope you know exactly what you do with
- Only node packages installed with npm or yarn are supported (which means only standardized NPM paths are supported)
Possible extra features
- Custom environment variable with custom path to resolve global modules.
Released under MIT license