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.
You can put this line in your
.profile
file:This will make
node
use the global path.As per documentation, Node.js will search in the following locations by default:
Path specified in the
NODE_PATH
environment variable.Note:
NODE_PATH
environment variable is set to a colon-delimited list of absolute paths.Current
node_modules
folder. (local)$HOME/.node_modules
(global)Note:
$HOME
is the user's home directory.$HOME/.node_libraries
(global)$PREFIX/lib/node
(global)Note:
$PREFIX
is Node.js's configurednode_prefix
.To check the current value of
node_prefix
, run:Note: Prefix corresponds to
--prefix
param during build and it's relative toprocess.execPath
. Not to confuse with value from thenpm config get prefix
command.sourceIf the given module can't be found, that means it is not present in one of the above locations.
Location of global root folder where modules are installed can be printed by:
npm root -g
(by default the path is computed at run-time unless overridden innpmrc
file).Solution
You can try the following workarounds:
Specify your global module location in
NODE_PATH
environment variable. E.g.To test and print the value of
NODE_PATH
, run:For more permanent solution, link your
$HOME/.node_modules
global user folder to point to the root folder, by running this command:Then re-test it via:
echo 'require("forever")' | node
command.Temporary change the current folder to where the extension has been installed globally, before invoking the script. E.g.
Configure global installation destination in
npm
userconfig file (see:npm help 5 npmrc
) or byuserconfig
param (--prefix
).To display the current config, run:
npm config list
.To edit the current config, run:
npm config edit
.Specify the full path of node modules location when calling
require()
. E.g.Install the package to custom location, e.g.
However, the installation will go under
~/.node_modules/lib/node_modules/
, so the location still needs to be added.See: npm local install package to custom location
Create a symlink in the current folder from the location of the global package. E.g.
Apologies for the necromancy but I'm able to specify hard-coded paths to globally installed modules:
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.
For CLI utilities that depend on big modules, like
puppeteer
, I like to spawn anpm root -g
and use it to require the global module.After you install package globally you have to link the local project with global package
See here
You can use the package
requireg
to solve this problem:will do the trick.
Also, there's another module,
global-npm
, while specific to just using the globalnpm
, you can look at the short code and see how the technique works.