Recently I have created a new user on Windows 10 and removed the old user, after that when I try adding an global npm package it get installed on the previous user %appdata%/Roaming directory. I'm not able to use those global libs now.
Say for example
npm install -g gulp
installs the gulp module under
C:\Users<DELETED_USER>\AppData\Roaming\npm\gulp
and not found in the new user directory
C:\Users<NEW_USER>\AppData\Roaming\npm\gulp
Is there any npm config I have to touch to fix this issue?
When you run the following command:
npm config get prefix
it probably prints:
C:\Users\<DELETED_USER>\AppData\Roaming\npm\
If that's the case, i.e. it references the pathname to the <DELETED_USER>, then consider utilizing the npm config command to change the prefix value. E.g.
npm config set prefix "C:\Users\<NEW_USER>\AppData\Roaming\npm"
If you also run the following command:
run npm config ls -l
you may find other configuration properties (e.g. cache) whose values contain the <DELETED_USER> pathname in them too. For those also consider setting them as necessary using: npm config set <key> <value>
Related
For instance, npm install -g sinopia
On windows7, I will install the sinopia command and related modules inside C:\Users\xxxx\AppData\Roaming\npm.
On Redhat5, my node and npm command are in /usr/local/clo/ven/node-v4.2.3-linux-x64/bin. When I run 'npm install -g sinopia', by default, sinopia was got installed in the current directory as npm and node like below.
But currently I got a linux machine that has got sinopia installed by other person. I can not find the start script of sinopia inside node/bin, and I can find sinopia related stuff like below.
.
Where can I find the start script of sinopia? Whether the installation location of the 'npm install -g xxx' can be configured?
npm installs packages globally to its set global "root". To find what the global root is in a given environment, run npm root -g.
It will typically be inside the "prefix" directory, which you can find with npm prefix -g.
Note you can also change the prefix directory with npm config -g set prefix </new/prefix/path>.
To answer your more specific question
In order to find the sinopia executable, you can run which sinopia on linux (you might need to install which on RedHat, it should be available in your package sources). It will give you the pathname of the file that would be executed for the sinopia command.
But that could be a symlink to another location; to resolve the pathname you can use readlink -f $(which sinopia) on bash. The -f option tells readlink to follow links recursively. $(which sinopia) will be substituted by the output of the which sinopia command.
Quick edit as I see this has already been answered above.
To find the install location of packages installed globally through npm, run the following:
npm config get prefix
to update this you can use the following command:
npm config set prefix path
Source: npm global path prefix
On Ubuntu 12.04 x 64...
npm -g install hiredis redis
Installs fine and npm ls shows those modules, but only when I'm in node source directory
does not show when I'm in any other directory
For kicks, tried running the command while in that other directory- still no dice :(
They are installing globally, but you cannot see them with npm ls, in other directories. Because npm ls only shows local modules. If you want to list global modules you have to type: npm ls -g.
Sometimes another version or just a wrong path is referenced in the npm config file instead of the installed version.
This may cause node/npm to misplace global modules.
To check and fix:
In cmd line type: npm config list
You should get a list of configuration values, one of them is prefix.
Make sure the path in prefix is the same path (only without node.exe) as the actually installed node.exe path.
(this path is listed further down as node bin location)
If it's not, change it:
Either in the config file (in your user folder, named .npmrc)
Or, via cmd line: npm config set prefix "C:\Program Files\nodejs" (change path for ubuntu of course)
Reinstall the module/package you tried to install, don't forget -g for global.
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.
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
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.
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 configured node_prefix.
To check the current value of node_prefix, run:
node -p process.config.variables.node_prefix
Note: Prefix corresponds to --prefix param during build and it's relative to process.execPath. Not to confuse with value from the npm config get prefix command.source
If 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 in npmrc file).
Solution
You can try the following workarounds:
Specify your global module location in NODE_PATH environment variable. E.g.
echo 'require("forever")' | NODE_PATH="$(npm root -g):$NODE_PATH" node
To test and print the value of NODE_PATH, run:
echo 'console.log(process.env.NODE_PATH); require("forever")' | NODE_PATH="$(npm root -g):$NODE_PATH" node
For more permanent solution, link your $HOME/.node_modules global user folder to point to the root folder, by running this command:
ln -vs "$(npm root -g)" "$HOME"/.node_modules
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.
npm install -g forever
cd "$(npm root -g)"
echo 'require("forever")' | node
cd -
Configure global installation destination in npm userconfig file (see: npm help 5 npmrc) or by userconfig 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.
require("/path/to/sub/module")
Install the package to custom location, e.g.
npm install forever -g --prefix "$HOME"/.node_modules
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.
npm link forever
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: https://github.com/npm/npm/blob/master/package.json). 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.
You can use the package requireg to solve this problem:
var forever = require('requireg')('forever')
will do the trick.
Also, there's another module, global-npm, while specific to just using the global npm, you can look at the short code and see how the technique works.
For CLI utilities that depend on big modules, like puppeteer, I like to spawn a npm root -g and use it to require the global module.
try {
const root = require('child_process').execSync('npm root -g').toString().trim()
var puppeteer = require(root + '/puppeteer')
} catch (err) {
console.error(`Install puppeteer globally first with: npm install -g puppeteer`)
process.exit(1)
}
You can put this line in your .profile file:
export NODE_PATH="$(npm config get prefix)/lib/node_modules"
This will make node use the global path.
i tried following the other answers but what worked for me was
node_path = "C:\\Users\\{usename}\\AppData\\Roaming\\npm\\node_modules"
const *modulename* = require(node_path + "\\" +'*modulename*');
npm install express -g
cd ~/mynodeproject/
npm link express
This will link your local project folder to global node_modules.
I am having a problem installing global node modules and everything I find online says the solve is just adding -g. Which is not the problem. I believe it's a linking issue or wrong directory issue.
Here is what I do:
$ npm install -g express
npm http GET https://registry.npmjs.org/express
npm http 304 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/range-parser/0.0.4
npm http GET https://registry.npmjs.org/mkdirp/0.3.3
...downloads correctly
$ express myapp
bash: express: command not found
However when I run the direct link location to express it works:
$ /usr/local/share/npm/bin/express myapp
create : myapp
create : myapp/package.json
create : myapp/app.js
... Builds app correctly
Where the module is:
$ which node
/usr/local/bin/node
$ node -pe process.execPath
/usr/local/Cellar/node/0.8.20/bin/node
$ npm link express
/Users/bentonrr/Development/Personal/node_modules/express -> /usr/local/share/npm/lib/node_modules/express
In my .bash_profile I have:
export PATH=/usr/local/bin:$PATH
export NODE_PATH=/usr/local/lib/node_modules:/usr/local/lib/node
Do I need to change my Node environment to download to correct folder? Is something not linking correctly? I am lost..
Thanks!
Other Specs:
$ node --version
v0.8.20
$ npm --version
1.2.11
$ brew --version
0.9.4
OSX Version 10.8.2
This may mean your node install prefix isn't what you expect.
You can set it like so:
npm config set prefix /usr/local
then try running npm install -g again, and it should work out. Worked for me on a mac, and the solution comes from this site:
http://webbb.be/blog/command-not-found-node-npm/
EDIT: Note that I just came across this again on a new Mac I'm setting up, and had to do the process detailed here on stackoverflow as well.
Add $(npm get prefix)/bin to your PATH (e.g., in .bashrc), like so:
echo "export PATH=$PATH:$(npm get prefix)/bin" >> ~/.bashrc
For more info, see npm help npm:
global mode:
npm installs packages into the install prefix at prefix/lib/node_modules and bins are installed in prefix/bin.
You can find the install prefix with npm get prefix or npm config list | grep prefix.
My npm couldn't find global packages as well. I did what Brad Parks suggested:
npm config set prefix /usr/local
Then I got a EACCES permissions error (DON'T USE sudo npm install -g <package>) and fixed it through the official npm docs: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
On the command line, in your home directory, create a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
In your preferred text editor, open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
On the command line, update your system variables:
source ~/.profile
Then install a package globally and test it! For example:
npm install -g awsmobile-cli
awsmobile configure
In my case, The NODE_PATH environment variable was empty. Check whether it is empty-
echo $NODE_PATH
if the NODE_PATH is empty. Then change ~/.bash_profile and add NODE_PATH
nano ~/.bash_profile
export NODE_PATH=`npm root -g`
source ~/.bash_profile
Now install npm modules again and check whether that is being installed on the path npm root -g
For Windows users
Add this to your path: "%AppData%\npm"
I do not ever install any npm stuff, via sudo! I have my own reasons, but I just try to keep things simple, and user based, since this is a user development world, and not everyone has root access, and root/sudo installing things like this just seems to clutter up things to begin with. After all, all developers should be able to follow these instructions, not just privileged sudo users.
This particular system is a RHEL7 accessed via SSH:
Frequently one needs various versions of node, so I use NVM https://github.com/creationix/nvm
So with that said, I can show you a working example for -g global installs, using NVM, NPM, and node paths not using root.
set your prefix for .npm-packages if it isn't already. (note, thats a hyphen, not an underscore)
nvm config ls
prefix = "/home/<yourusername>/.npm-packages"
Then adjust your ~/.bash_profile or .bashrc if you prefer readup on why and which here, with the following information.
#PATH EXPORTS
NODE_MODULES=$HOME/.npm
NPM_PACKAGES=$HOME/.npm-packages/bin
export PATH=$PATH:$HOME/bin:$NODE_MODULES:$NPM_PACKAGES
#NVM ENABLE
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
That pretty much covers all paths. For e.g., if you install gulp like this npm install -g gulp it symlinks in ~/.npm-packages/bin (note thats a hyphen, not an underscore). (no need for gulp-cli, or gulp-cl)
You can pretty much replace/comment-out all other node path exports. You can put this path info below any other path info you already have, safely, without it overwriting that stuff.
Check your global Node module's binary folder, and add it to your $PATH.
npm list -g | head -1
If you use nodenv, the path will change whenever you install a new global node version. Adding a node path like this solves my problem.
"$HOME/.nodenv/versions/$(nodenv global)/bin"
Shortcut for adding the path to zsh
$ echo 'export PATH="$HOME/.nodenv/versions/$(nodenv global)/bin"' >> ~/.zshrc
Add the following line to your ~/.bash_profile
export PATH="$HOME/.npm/bin:$PATH"
Load bash profile
bash -l
The problem I had was missing the binaries because the user specific .npmrc file in my home directory had bin-links set to false, though the default is true.
Just in case this is your problem check that none of your .npmrc files have it set to false.
Then re-installing all modules will create the binaries at the prefix so your PATH can see them.
It may seem like a hack, but setting up yarn when possible saves you a lot of node environment headaches for various unix distros.
I've installed Node.js on my Windows 7 x64 development machine, the manual way:
mkdir C:\Devel\nodejs
cd C:\Devel\nodejs
set NODE_PATH=%CD%
setx /M PATH "%PATH%;%NODE_PATH%"
setx /M NODE_PATH "%NODE_PATH%\node_modules"
I've placed the main node x64 binary along with npm package manager in C:\Devel\nodejs. Works like a charm and I can update the main binary without dealing with the installer.
The only problem I can't solve is moving the cache folder. When I install a local package:
npm install express
... cache is placed under %APP_DATA%\npm-cache folder. I'd like to change it to:
C:\Devel\nodejs\npm-cache
How can I change the npm cache folder, or disable it completely?
You can change npm cache folder using the npm command line. (see https://docs.npmjs.com/cli/v6/using-npm/config#cache)
So you might want to try this command :
> npm config set cache C:\Devel\nodejs\npm-cache --global
Then, run npm --global cache verify after running this command.
You can also set an environment variable with export npm_config_cache=/path/to/cache (Unix) or set npm_config_cache=C:\path\to\cache (Win) as an alternative to npm config set (this is true for all config options in npm).
For anyone using docker you can add the env var at runtime with:
docker run -e npm_config_cache=/path/to/cache mydockerimage:tag
You can also do following:
For having cache path as you wish, for a single package while installing it:
npm install packageName --cache path/to/some/folder
For having cache path as you wish, for all the packages in package.json:
Just be in the directory where package.json is as usual and do
npm install --cache path/to/some/folder
You may not find this in npm documentation but i have tried it with npm 6 and it works.
Looks like it works since npm 5 [Refer: How to specify cache folder in npm5 on install command?
In Windows you can simply cd to the desired cache folder and do npm set cache --global
Solution
Paste the following code into npmrc file.
Location of npmrc file: C:\Program Files\nodejs\node_modules\npm\npmrc
prefix=D:\nodejs\npm
cache=D:\nodejs\npm-cache
Notes:
There is no '.' in front of npmrc
Diagrams
NPMRC file folder look like this
NPMRC Content look like this
Hope it helps. Cheers
In addition, I found that running an update command works also - for example:
npm update npm
Lastly, one can check their npm-cache directory to see if is being filled or not.