Executing local (--save-dev) tools by npm - node.js

I just installed bower via npm install bower --save-dev because I want to have this available for everybody who checks the branch out and runs "npm update". I knew how to execute bower if I installed it with npm install bower -g but how do I actually execute it now?
this way does not satisfy me:
./node_modules/bower/bin/bower -v
Isn't there something like npm tools bower -v?

No, there is no npm tools command. But here is an alternative solution.
If your shell is bash, you can add function into your .bashrc.
# ~/.bashrc
function npm-tools {
(PATH=$(npm bin):$PATH; eval $#;)
}
Then you can run command like following to achieve what you want.
# $ tree node_modules/ -L=1
# node_modules/
# └── bower
$ ./node_modules/bower/bin/bower -v
1.7.9
$ npm-tools bower -v
1.7.9

Related

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.

Node.js dependency not found even if installed

I'm trying to minify a whole folder of css files using minify-all.
But when I run: minify-all, I get:
Air-di-Giacomo:css jack$ sudo minify-all .
found file: alpha.css
/usr/local/lib/node_modules/minify-all/node_modules/node-minify/lib/node-minify.js:104
throw new Error(bin + ' not found !');
^
Error: uglifyjs not found !
at getPath (/usr/local/lib/node_modules/minify-all/node_modules/node-minify/lib/node-minify.js:104:17)
at Object.minify.fn.compress (/usr/local/lib/node_modules/minify-all/node_modules/node-minify/lib/node-minify.js:132:27)
at Object.minify (/usr/local/lib/node_modules/minify-all/node_modules/node-minify/lib/node-minify.js:83:10)
at /usr/local/lib/node_modules/minify-all/index.js:30:13
at /usr/local/lib/node_modules/minify-all/index.js:14:13
at Array.forEach (native)
at walk (/usr/local/lib/node_modules/minify-all/index.js:10:36)
at minifyAll (/usr/local/lib/node_modules/minify-all/index.js:25:5)
at Object.<anonymous> (/usr/local/lib/node_modules/minify-all/index.js:45:5)
at Module._compile (module.js:409:26)
I already tried the following commands:
sudo npm install -g uglifyjs
and also to reinstall it in the correct order (never know):
sudo npm remove -g minify-all
sudo npm remove -g uglifyjs
sudo npm install -g uglifyjs
sudo npm install -g minify-all
and finally, in despair, I did:
cd /usr/local/lib/node_modules/minify-all/
sudo npm install uglifyjs
But I still get that error!
This seems to me a node dependency problem, and maybe for some of you it is a stupid problem, do you know how can I solve this?
Thanks in advance.
Found a solution by install it locally.
cd /../myCssFolder/
npm init
...
npm install uglifyjs
npm install minify-all
minify-all .
of course I prefer to have it globally so I can skip the npm project installation everytime... Any suggestions?
Global modules always install in NODE_PATH/bin。
why not print echo $path in terminal, Is there NODE_PATH/bin in it ?
if not, you can update ~/.bashrc(if you use bash) or ~/.zshrc(if you use zsh)
export PATH="OLD_PATH:NODE_PATH/bin"
The OLD_PATH is the older one.

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

npm upgrade with homebrew

When node (v.0.10.33) is installed with homebrew (v. 0.9.5), at one point it says:
==> Caveats
If you update npm itself do NOT use the npm upgrade command
Instead execute:
npm install -g npm#latest
So, what exactly is npm upgrade and what is the difference with npm install -g npm#latest?
-- edit 2015
the problem doesn't exist anymore with the current version of node. (but I never had an answer to what npm upgrade is?)
Use npm install to install a package and npm update to update a package.
That Homebrew npm caveat was removed after the issue with npm update -g was fixed.
npm comes bundled with node, both part of Node.js install --- no need to install separately
Below are the steps to install Node.js from source (OSX/linux)
Issue cmds as yourself NOT root (sudo)
to start fresh remove prior node and npm installs as well as these :
sudo mv ~/.npmrc ~/.npmrc_ignore
sudo mv ~/.npm ~/.npm_ignore
sudo mv ~/tmp ~/tmp_ignore
sudo mv ~/.npm-init.js ~/.npm-init.js_ignore
download source from : http://nodejs.org/download/
cd freshly-downloaded-dir
define environment variable NODE_PATH as the dir for subsequent module installs
export NODE_PARENT=${HOME}/nodejs-v0.10.33
export PATH=${NODE_PARENT}/bin:${PATH}
export NODE_PATH=${NODE_PARENT}/lib/node_modules
./configure --prefix=${NODE_PARENT}
make
make install # IMPORTANT this is NOT using sudo
# not wanted since installing into $USER owned $NODE_PARENT
which puts it into dir defined by above --prefix
when you use syntax : npm install -g some_cool_module
the -g for global installs it into dir $NODE_PATH and not your $PWD
Now put above three export xxx=yyy
commands into your ~/.bashrc or some such to persist these environment variable changes

How do I know whether I have Node.js and npm successfully installed on Ubuntu 14.04?

I installed Node.js with these instructions and it seemed successful:
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
Then I installed npm with these instructions:
sudo curl https://www.npmjs.org/install.sh | sh
The nodejs installation seemed to work without errors but the npm command gave me a lot of errors. But it seems like they are installed because when I test what version I have they both come up:
nodejs -v
v0.10.30
npm -v
1.4.21
So If this doesn't tell me that I have both programs successfully installed, which I assume I do not, how do I know?
I think your tests tell that both or properly installed.
But you can try just type node in terminal & it should open a node shell, where you can check by running basic commands.
Current distributions of node.js (including the one you downloaded) already include npm. So maybe installing npm manually is one source of your errors. Beware that usually you run "npm install" with the permissions of a regular user. There are only some npm-based utilities that are to be installed with root permissions and the '-g' (global) command line switch.
On linux if you wish to install node.js and npm as yourself NOT root :
to start fresh remove prior node.js and npm installs as well as these :
~/.npmrc
~/.npm
~/tmp
~/.npm-init.js
create your ~/bin/ directory if not already created :
mkdir ${HOME}/bin
download source from : http://nodejs.org/download/
cd node-v0.10.30/
./configure --prefix=${HOME}/bin/nodejs
make -j8
make install
which puts it into dir defined by above --prefix
export PATH=${HOME}/bin/nodejs/bin:$PATH
define NODE_PATH so node can find dir for modules otherwise
npm install xxx will put newly installed module into dir in curr dir :
export NODE_PATH=${HOME}/bin/nodejs/lib/node_modules
do above AND use syntax :
npm install xxxxx -g
always use the -g for global which puts package xxxxx into $NODE_PATH
NOTE - nodejs install gives you npm as well :
ls -la ${HOME}/bin/nodejs/bin

Resources