NPM installing to ~/.npm directory - node.js

I have to run npm install --prefix ./ --save bootstrap jquery for it to save in the node_modules. I expect from what I've read in tutorials and such this is not the expected behavior. When I just run npm install --save bootstrap it puts the files into /home/philip/.npm directory as /home/philip/.npm/bootstrap.
npm root gives the correct [project]/node_modules directory.
NPM Version: 3.10.10
Node Version: 6.10.3
OS: Ubuntu 17.04
Edit: Forgot to ask the question, how do I ensure npm install defaults to the project's node_modules directory?

It might be because global flag is set to true somewhere in npm config or via an environment variable.
To check current value run: npm config ls -l | grep global. To change it try to add global=false to your ~/.npmrc file. Also, check the value of $NPM_CONFIG_GLOBAL, it has higher priority than .npmrc.

Related

npm - set project specific default prefix/path

I would like npm ... to always run in the context of a subfolder of my project: "./assets".
When I run this command from the project root, it behaves as expected:
$ npm --prefix ./assets install
However, it does not read this from the .npmrc in the root folder.
$ echo "prefix=./assets" > .npmrc
$ npm i
# creates an empty ./node_modules folder
How can I set a set project specific default prefix for npm commands?
If you have install npm globally you can simply configure prefix to set for specific folder so that you can install other node modules to that folder. Following is the command to set prefix.
npm config set prefix /Programming/node-v0.10.31-linux-x64/
here i have configure Node in node-v0.10.31-linux-x64 this folder and node modules will be install there.
or
if you want to install node modules for specific folder individually you can user following command
npm install bower -g --prefix /Programming/node-v0.10.31-linux-x64/

what's the destination does 'npm install -g xxx' install to?

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

Cannot run ionic. receives "No command 'ionic' found"

I want to start using the ionic framework, but unfortunately I'm already failing on the first step.
I am running Ubuntu 13.04 and I have node v0.10.25 installed.
I've installed ionic, at described in their docs:
sudo npm install -g cordova
sudo npm install -g ionic
The installation went well, no errors or warnings, but after the installation I type
ionic
and I get the error:
No command 'ionic' found, did you mean:
Command 'ionice' from package 'util-linux' (main)
Command 'sonic' from package 'sonic' (universe)
ionic: command not found
I'm pretty new to ubuntu so I might have something not configured correctly, but I can't find what.
Thanks
for some of you, the two answer above might not work. here's a more general solution for situation where you see "XX" command not found
first check your npm root and npm root -g
the result for the npm root -g should be something like "/usr/local".
if it's not, then you found your problem.
change it by:
npm config set prefix /usr/local
then npm root -g should give you something like /usr/local/lib/node_modules
.
Then go ahead re-install everything with -g
you will be good to go!
Well, I found it finally.
The ionic installation was at /home/guy/npm/bin/ionic, not at /usr/bin/ionic at it should be.
Solved it with:
sudo ln -s /home/guy/npm/bin/ionic /usr/bin/ionic
I solved the problem by cd to my root. Then install ionic as root admin.
$ sudo npm install -g cordova ionic
then run
$ ionic
to see if it's working.
Someone might run into this after trying to change the global library directory of npm to a folder they have permissions to write to in order to be able to install global libs without root permissions.
In that case you might have forgotten to add the new folder to you PATH environment variable.
The whole process of fixing Permissions can be found here.
in case that source disappears here is a copy of the steps:
There are times when you do not want to change ownership of the
default directory that npm uses (i.e. /usr) as this could cause some
problems, for example if you are sharing the system with other users.
Instead, you can configure npm to use a different directory
altogether. In our case, this will be a hidden directory in our home folder.
Make a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
Open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
Back on the command line, update your system variables:
source ~/.profile
Test: Download a package globally without using sudo.
npm install -g jshint
Instead of steps 2-4 you can also use the corresponding ENV variable (e.g. if you don't want to modify
~/.profile):
NPM_CONFIG_PREFIX=~/.npm-global npm install -g jshint
if you just follow these steps and reinstall all the Global libs there is a good chance that it will start working for you anyways...
Just remember that if you do this, you will save your global libs into the folder created in step 1 instead of the default location in /usr/local or just /usr (depending on your OS flavor i guess?)
First Solution
I recently ran into this issue and the only solution that worked for me was to uninstall both ionic and cordova.
npm uninstall -g cordova
npm uninstall -g ionic
Then just reinstall
npm install -g cordova
npm install -g ionic
Second Solution
I ran into this issue again! This time check your environmental variables.
Run npm bin -g and check if the path returned is in your environmental variables. For me, it prompted that it was not found in the terminal output. I added that path to the environmental variables and restarted the terminal. From there everything started to work again. (source)
I had the same problem with “bash: ionic: command not found”,
then I added:
%USERPROFILE%\AppData\Roaming\npm
to my environment variable’s path, then I reinstalled ionic and cordova and it start working.
For Mac users
1. install the ionic
npm i -g #ionic/cli
2. check npm path
npm root -g
3. copy full path of ionic
Ex. /usr/local/Cellar/node/15.2.1/lib/node_modules/#ionic/cli/bin
4. open this file as administrator "/etc/paths" with nano or whatever editor
Ex. sudo nano /etc/paths
5. Add ionic bin file full path to file ends. And save file.
That's it.
Run npm root -g, copy the result path and add it to the paths file:
sudo nano /etc/paths
Restart your console and it will work.
this worked for me. try adding below to the ~/.bash_profile for mac OSX el capitan users:
NPM_PACKAGES="${HOME}/.npm-packages"
NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
PATH="$NPM_PACKAGES/bin:$PATH"
# Unset manpath so we can inherit from /etc/manpath via the `manpath`
# command
unset MANPATH # delete if you already modified MANPATH elsewhere in your config
MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
then run source ~/.bash_profile to reload the profile in terminal.
additional info: node v4.3.1
In my case, I have just uninstalled ionic and then reinstalled it. And it is working fine now.
I also faced same problem but i solved i used following commands on terminal it worked
sudo npm uninstall ionic
then i used
sudo npm install -g cordova ionic#latest
and it worked fine it automatically installs best stabel version of cordova and latest stable version of ionic . for me it installed ionic 3.7.0
and cordova 7.0.1
you can change temporary :
npm config set prefix C:\Users[username]\AppData\Roaming\npm\node_modules2
change the Path in Environment Variables set C:\Users[username]\AppData\Roaming\npm\node_modules2
Run your command to install your package .
open file explorer copy the link C:\Users[username]\AppData\Roaming\npm\node_modules
ok file yourpackage.CMD created another folder Created "node_modules2" in node_modules and contain your package folder.
copy your package file CMD to parent folder "npm"
copy your package folder to parent folder "node_modules"
now run npm config set prefix C:\Users[username]\AppData\Roaming\npm
change the Path in Environment Variables set C:\Users[username]\AppData\Roaming\npm
now package working correctly with Command line
=> this method working with me When npm block in installation Package for IONIC installation and ReactNative and another package npm.
The package name has changed from ionic to #ionic/cli!
To update, run: npm uninstall -g ionic
Then run: npm i -g #ionic/cli

Modules are not installing globally with "npm -g install..."

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.

How to npm install to a specified directory?

Is it possible to specify a target directory when running npm install <package>?
You can use the --prefix option:
mkdir -p ./install/here/node_modules
npm install --prefix ./install/here <package>
The package(s) will then be installed in ./install/here/node_modules. The mkdir is needed since npm might otherwise choose an already existing node_modules directory higher up in the hierarchy. (See npm documentation on folders.)
As of npm version 3.8.6, you can use
npm install --prefix ./install/here <package>
to install in the specified directory. NPM automatically creates node_modules folder even when a node_modules directory already exists in the higher up hierarchy.
You can also have a package.json in the current directory and then install it in the specified directory using --prefix option:
npm install --prefix ./install/here
As of npm 6.0.0, you can use
npm install --prefix ./install/here ./
to install the package.json in current directory to "./install/here" directory. There is one thing that I have noticed on Mac that it creates a symlink to parent folder inside the node_modules directory. But, it still works.
NOTE: NPM honours the path that you've specified through the --prefix option. It resolves as per npm documentation on folders, only when npm install is used without the --prefix option.
In the documentation it's stated:
Use the prefix option together with the global option:
The prefix config defaults to the location where node is installed. On
most systems, this is /usr/local. On windows, this is the exact
location of the node.exe binary. On Unix systems, it's one level up,
since node is typically installed at {prefix}/bin/node rather than
{prefix}/node.exe.
When the global flag is set, npm installs things into this prefix.
When it is not set, it uses the root of the current package, or the
current working directory if not in a package already.
(Emphasis by them)
So in your root directory you could install with
npm install --prefix <path/to/prefix_folder> -g
and it will install the node_modules folder into the folder
<path/to/prefix_folder>/lib/node_modules
There currently is no documented way to install a package in an arbitrary directory. You should change into the target directory, make sure it has a package.json, and then use the regular commands.
While there currently is an undocumented option called --prefix, this feature might be removed in a future release. At least, it it is not planned to ever document this as an official feature.
I am using a powershell build and couldn't get npm to run without changing the current directory.
Ended up using the start command and just specifying the working directory:
start "npm" -ArgumentList "install --warn" -wo $buildFolder

Resources