NPM global packages not available on Ubuntu - node.js

MY SITUATION
I have installed version Node v6.2.0 using n on Ubuntu 14.04. It's located under path /usr/local/n/versions/node/6.2.0 (by using npm prefix -g).
My global packages installs in that directory under lib/node_modules but I can't use that packages from command line globally.
NOTE
Earlier I was using older version Nodejs 5 installed different way. And packages installed globally with that version of Node are available now, but not new ones which I installing now.
MY QUESTION
Is there a trick or something to point node_modules directory from which its packages could be used globally?

Your global package binaries should be available here:
/usr/local/n/versions/node/{NODE VERSION}/bin
Add it to your path (in .bashrc or .bashprofile or .zshrc):
NPM_GLOBALS=/usr/local/n/versions/node/{NODE VERSION}/bin
PATH=$NPM_GLOBALS:$PATH
Then restart your console or just run source ~/.bashrc
Also, if you're working with multiple versions of node, I highly recommend nvm for installing and managing them.

I simply added export PATH=~/.npm-global/bin:$PATH to my .zshrc file.

Related

How to I Change node version in windows?

I'm Using nvm-windows.
Tried change node version, But It doesn't work for me.
It Seems like nvm is not change global node.
How Can I switching node version?
Install nvm. In nvm you can install all node version, and you can change it very easy. :)
Example
nvm list
nvm install 10.0.0
nvm list
nvm use 10.0.0
And you using node 10.0.0 :)
Uninstall and reinstall nvm. Make sure to do this before reinstalling:
Uninstall existing node
Please note, you need to uninstall any existing versions of node.js before installing NVM for Windows. Also delete any existing nodejs installation directories (e.g., "C:\Program Files\nodejs") that might remain. NVM's generated symlink will not overwrite an existing (even empty) installation directory.
Uninstall existing npm
You should also delete the existing npm install location (e.g. "C:\Users<user>\AppData\Roaming\npm"), so that the nvm install location will be correctly used instead. Backup the global npmrc config (e.g. C:\Users<user>\AppData\Roaming\npm\etc\npmrc), if you have some important settings there, or copy the settings to the user config C:\Users<user>.npmrc.

Why do I need to uninstall node from my computer to use nvm?

I recently decided to install nvm, and the instructions had me uninstall node. I noticed that if I explicitly tell nvm to use a certain version it uses it from the nvm directory, otherwise it uses my bin/node directory, so I didn't notice a conflict. When I uninstalled node, i needed to specify a version every time I use my terminal which seems like an unnecessary step when I could just leave node installed via brew and use nvm if I need to version control.
Once you install nvm and use Node through it, it does NOT use bin/node, but the version installed with nvm, so your old Node is obsolete.
The main reason they recommend you to uninstall Node is because it could confuse the shell about which Node to use. Of course, you can update your shell config so that it first looks for Node installed from nvm, but that's an unnecessary extra step and your old Node distribution is obsolete on your disk, at best.
So, the best solution is uninstalling the old distribution and install nvm (which by default installs the latest Node in your .nvm folder), and install more versions of Node if necessary.
And regarding this:
i needed to specify a version every time I use my terminal which seems like an unnecessary step
You don't need to, as long as you set the default version (say, 11.15.0) config after installing nvm with the following command:
nvm alias default 11.15.0

Running local node verison different than global with gulp and node-sass

I have a project that is using node npm and gulp. When we build the project node 5 is installed in the local directory and runs npm install (compiling all node modules agains node v5) I globally have node version 7 installed.
We have a gulp task that uses node-sass which is compiled against v5 but when I try to run the task it uses the global version and node-sass errors out out "Missing binding"
How can I run it using the local version of node?
nvm is very helpful for managing and switching between multiple versions of node, including your globally installed packages.
If you are looking to automate it, you could just add the console command to use the needed version via nvm within your gulp task.
e.g. "nvm install v5.0.0"
To expand upon #dmfay's answer, you should have node 5 installed (via nvm or tj's n).
However you can include in your package.json for the project such that it relies on node 5. (Under the "engine" property)
None of the install/rebuild solutions resolved the issue for me (using gulp).
Here is how I resolved it:
1) Download the missing binding file from the repository.
2) Rename the file binding.node.
3) Create /node-modules/vendor/<operating system>/ (for my version of node/node-sass the <operating system> directory for linux is linux-x64-64, use folder name from missing binding error message)
4) Add binding.node file to /node-modules/vendor/<operating system>/.
5) run gulp

Unable to remove NodeJS

It says that node is installed in my /home/raheel/.nvm/v0.10.40/bin/node.
But it does not remove node with apt-get remove node.
I exactly don't remember how did i install it as i was following a tutorial.
Thanks
nvm stores copies of node in a different place than the copy that your package manager installs. Also, it should be noted that the node package in Debian/Ubuntu is not node.js, but instead an amateur radio program (the actual package name is nodejs/nodejs-legacy).
So to remove nvm's copy of node, you could do nvm uninstall 0.10.40 to uninstall that particular version or you can do nvm deactivate to remove nvm's node binaries from your $PATH entirely.

Locally installed versus globally installed NPM modules

In my package.json file, I have bower listed as a dependency. After I run npm install, bower gets installed locally. When I try to run bower after installing it locally I get an error
"bower" is not recognized as an internal or external command
It seems the only way to resolve this is to install bower globally. Why should I have to do this? If my project contains a local copy of bower, why won't node use it?
Installing locally makes bower available to the current project (where it stores all of the node modules in node_modules). This is usually only good for using a module like so var module = require('module'); It will not be available as a command that the shell can resolve until you install it globally npm install -g module where npm will install it in a place where your path variable will resolve this command.
Edit: This documentation explains it pretty thorougly.
You can execute your local instance by typing the line below in cmd:
node_modules/bower/bin/bower <bower args>
We use both PHP and JavaScript, so we have composer and npm.
Each of the projects we work on have different packages both for runtime of the package as well as build/dev tools.
As there are version constraints in each project, installing version x of a package globally (that would be run from the command line), would cause us issues, we install all the tooling in each package. Much easier to define in the appropriate composer.json / package.json files.
But running the CLI tools is a pain if you have to constantly add an additional path to the command.
To that end, we have recommend to the team that the following paths are added to your $PATH in the appropriate .bashrc (or equivalent):
./vendor/bin:./node_modules/.bin
(EDIT: For Windows, the paths would be .\vendor\bin;.\node_modules\.bin;)
So, whilst in project X, we have access to the CLI tools for that project. Switch to project Y, and we get that projects tools.
Sure, you are going to get duplications, but each project is maintained by different teams (and some people are in multiple teams), so again, having 1 version in the global setup is an issue there.
Usually you install NPM modules globally if you want them included in your path to be ran from the command line. Since it is installed locally you will have to run it from the node_modules folder.

Resources