npm - set project specific default prefix/path - node.js

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/

Related

Set directory for NPM

I'm working with git-bash on win 7. I want to develop an ember project and I am working on a thumb drive. I have installed node on my E drive and added the path to the env vars.
$ npm install -g ember-cli
C:\Users\me\AppData\Roaming\npm\ember -> C:\Users\me\AppData\Roaming\npm\node_modules\ember-cli\bin\ember
C:\Users\me\AppData\Roaming\npm\� -> C:\Users\me\AppData\Roaming\npm\node_modules\ember-cli\bin\ember
+ ember-cli#2.16.2
updated 553 packages in 89.25s
$ which npm
/e/nodejs/npm
$ which node
/e/nodejs/node
$ ember build
sh.exe": ember: command not found
So I think whats happening is that npm is installing the packages on the c drive. I need to set a folder on my e drive, to make this portable. How can I do this?
You can set this by configuring the npm prefix setting:
npm config set prefix e:\some\path\on\e
This could be done globally or on a per-project basis by modifying the project's .npmrc file.
Go to the folder with your package.json and run npm config ls to see your effective npm config.
See the npm docs for global settings

NPM installing to ~/.npm directory

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.

npm install -g is copying all my source code to the prefix folder (Windows)

I'm building a .NET Web Application and using NodeJS for a bunch of build tasks (minification, bundling, unit tests, etc).
My problem is that when I run the following from my source code folder:
npm install -g
It not only checks and installs all of the node packages (great!) .. but it also copies the ENTIRE source code folder over to my global node_modules folder (.cs, .sln, bin folders .. the lot ... not so great!)
Is there any way of stopping it from doing this?
Note - I've redirected the npm and cache folders to a separate path using:
npm config set prefix e:\npm-repo\npm --global
npm config set cache e:\npm-repo\cache --global
This is how npm install -g works.
You can try npm install if you want to install only the dependencies from package.json locally.

Install for #angular/cli not working on Mac

I'm trying to setup Angular 2 using "npm install #angular/cli -g "
After the install, the only warning I see is the UNMET PEER DEPENDENCY rxjs#^5.0.1, which I then install and reinstall "npm install #angular/cli -g"
No matter what I do, or what version of Node I setup with n, I keep getting the following message when trying to user the "ng" commands:
zsh: command not found: ng
I've been looking around and have not found a solution for this.
Has anyone run into this and have any suggestions?
UPDATE:
It looks like this is not a angular/cli specific issue.
I now see that I get the same message when I try to run "Grunt" and "Ionic" commands on an existing project that was working fine.
zsh: command not found: ionic
zsh: command not found: grunt
Most likely, the directory in which the global modules are installed is not in your $PATH -- and therefore unknown to your shell.
To fix this issue, we can create a new directory for global node_modules, configure npm to use it, and add that directory to your $PATH.
# create a new directory where npm will install packages
$ mkdir ~/.node_modules
# set npm "prefix" config to that directory
$ npm config set prefix '~/.node_modules'
# append a line to your .zshrc instructing it to include that directory in your $PATH, making the executables known to the shell
$ echo 'export PATH=~/.node_modules/bin:$PATH' >> ~/.zshrc
# update current shell with new path (not needed for new sessions)
$ source ~/.zshrc
Then, first reinstall the latest npm (npm i -g npm), followed by the global packages you need (npm i -g #angular/cli).
For more on PATH, see this definition: http://www.linfo.org/path_env_var.html

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