Installed node/npm on macOS Sierra into /usr/local/bin, not executable - node.js

Fresh install of macOS Sierra on a new MBP, /usr/local/bin requires root to access the files. But many programs, including node and npm install their executables to /usr/local/bin. I can't execute them unless I'm root. Am I supposed to change permissions on /usr/local/bin in macOS Sierra? Seems there's a reason for the stricter permissions.. Are tools like node/npm just not up to date with how things are done in the latest mac OS and I should move those files to somewhere else like /usr/bin?

Change npm's default directory to another directory that requires you not to run it as root
mkdir ~/my/path
npm config set prefix '~/my/path'
Open or create a ~/.profile file and add this line:
export PATH=~/my/path/bin:$PATH
Back on the command line, update your system variables:
source ~/.profile
Test: Download a package globally without using sudo.

Related

Node and npm not found after restarting macOS

I am using my corporate's laptop and am a new mac User (used Ubuntu before) :
OS: macOS Monterey Version 12.2
There are two accounts, administrator and mine - I don't have sudo rights. To install homebrew without administrator rights, I followed this Installation.
To install the node - I used brew install node. Both node -v and npm -v were working. When I restarted the laptop, I cannot find node/npm.
On running $ node -v, I get -bash: node: command not found (I changed my default terminal from zsh to bash and the output is the same for both of them)
I tried this solution but couldn't find nvm in the system. Am new to mac and I believe nvm is some kind of package manager like homebrew so this solution is not applicable to me (correct me if I am wrong).
How can I install things in my system without sudo rights and keep them permanently(like node)?
EDIT:
(After adding brew to the PATH) On running - brew list|grep node, I got - node
$ echo $PATH gives /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/parthkapadia/homebrew/bin
EDIT2:
Adding brew to path solved the issue, now I can even access node and npm (even after restarting). I used this site to add homebrew/bin to path (in zsh terminal)
The issue was homebrew's path. It was not added to the PATH variable.
When I restarted the system, homebrew was no longer in the PATH (as it was temporarily added probably when I installed it). As homebrew was not added to PATH, the terminal didn't recognize brew or any package installed using it like node or npm.
I solved it by adding Users/username/homebrew/bin to PATH. The steps I followed are -
cd - to move to the home directory
touch .zshrc to create .zshrc file as it didn't exist
nano .zshrc to open the file for editing
Added export PATH=$PATH:/Users/yourusername/homebrew/bin in the file (this appends homebrew/bin to the PATH variable)
Now the terminal can recognize brew and hence node and npm too.
Refer this for more detailed explanation on how to add to PATH in macOS.
Thanks to all the people who helped in the comments.

Node won't upgrade beyond v6.11.2

When I run node -v I see:
node -v
v6.11.2
I run brew update, brew upgrade, and update all npm modules every morning when I turn on my MacBook. I have npm 6.4.1.
I went to the Node downloads website, downloaded the installer, and installed v10.13.0. node -v still says v.6.11.2. I restarted my computer, ran the install again, no change.
I think I see the problem. When I run echo $PATH is see:
/Users/TDK/.rvm/gems/ruby-2.2.3/bin:
/Users/TDK/.rvm/gems/ruby-2.2.3#global/bin:
/Users/TDK/.rvm/rubies/ruby-2.2.3/bin:
/Users/TDK/.nvm/versions/node/v6.11.2/bin:
/usr/local/mysql/bin:/Users/TDK/google-cloud-sdk/bin:
/usr/local/heroku/bin:
/usr/local/bin:
/usr/bin:
/bin:
/usr/sbin:
/sbin:
/Users/TDK/.rvm/bin
/usr/local/bin is the path to Node, so that's good. But above it is a path to /Users/TDK/.nvm/versions/node/v6.11.2/bin. I need to get rid of that path. I opened /etc/paths and saw:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
It looks like I have another paths file somewhere. All those paths above /usr/local/bin were installed when I was in coding bootcamp, I can get rid of all of them. Where might this other paths file be? I tried Spotlight to search for the first path, it couldn't find any file containing that string.
It looks like these are all path for one specific user /Users/TDK/ (that's me), not global paths for all users. Is there somewhere in /Users/TDK/ with paths? I commented out and then deleted lines involving RVM in the following files, without fixing the problem.
~/.bashrc
~/.bash_profile
~/.profile
I checked these files but didn't see anything suspicious:
/etc/profile
/etc/bashrc
Another thought is that nvm is causing the problem. The path
/Users/TDK/.nvm/versions/node/v6.11.2/bin:
is the problem. Maybe instead of removing this path I can alter nvm to point to the latest version of node?
Do you want to remove node 6.11.2 from your computer?
If not, just run nvm install node. That will install the lastest node, if you want to install a specific version, run nvm install 10.10.0 instead
Then run nvm use node or nvm run node --version

node 5.5.0 already installed but node -v fetches with "v4.2.1" on OS X & homebrew?

I'm trying to update my nodejs v4.2.1 to the latest version and successfully got that. But node -v still gives me '-v4.2.1'.
Probably the older Node is in your PATH before the newer one.
You can run in your shell:
which node
to see where is the Node binary that is run by default (v4.2.1 in your case). You can see what is your PATH by running:
echo $PATH
It will show something like:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Those are directories that are searched in order whenever you type "node" or any other command. If your PATH doesn't have the directory where you have your new Node installed, or if it is after the directory where you have your old Node, then the new Node will not be run. Fixing the problem may be as simple as running:
PATH="/usr/local/bin:$PATH"
if your new Node is installed in /usr/local/bin (or with some other path if it is installed somewhere else). You need to add this line in .profile or .bashrc in your HOME to have the PATH set up correctly every time you log in start a new shell.
To see if you have the correct Node version in /usr/local/bin run:
/usr/local/bin/node -v
Update
Looking at your comment and updated answer my bet would be that you have installed Node 4.2.1 manually (not with brew) and now brew doesn't update the binary in /usr/local/bin.
What I would recommend is to install it manually and have control over the versions. I'll show you the commands to download the source, configure, build and install in a versioned directory, and update the PATH.
According to the Node download page the current version is v6.1.0 but if you want specifically 5.5.0 or any other version (the latest 5.x is v5.9.1) then just change the commands below to the verson that you want. (All versions are listed here.)
# change dir to your home:
cd ~
# download the source:
curl -O https://nodejs.org/dist/v6.1.0/node-v6.1.0.tar.gz
# extract the archive:
tar xzvf node-v6.1.0.tar.gz
# go into the extracted dir:
cd node-v6.1.0
# configure for installation:
./configure --prefix=/opt/node-v6.1.0
# build and test:
make && make test
# install:
sudo make install
# make a symlink to that version:
sudo ln -svf /opt/node-v6.1.0 /opt/node
and finally add PATH="/opt/node/bin:$PATH" to your .profile or .bashrc (The node-v6.1.0 directory in your HOME and the .tar.gz can be removed or kept for later use).
At this point which node should return /opt/node/bin/node and the version is the one that you want (6.1.0 in this example). If you want to test another version then install it in another directory under /opt/node-vXXX and update the symlink. You won't have to update PATH, just the symlink.
This is more work than with brew but you have total control over what gets installed and where. What I like about this solution is that your versions never get mixed or confused and you can install/remove versions any time and quickly set any version as default.
Your path contains /usr/local/bin before /usr/local/Cellar. Since the version of node you're using is the homebrew one, fix it with:
PATH="/usr/local/Cellar:$PATH"
Next time you have a problem with brew, run brew doctor, it would have told you what to do

Global npm install location on windows?

I'm not 100% sure, but I believe I installed node v5 from the windows installer on both my home and office PCs.
On my home PC global installs happen under %APPDATA%:
(dev) go|c:\srv> which lessc
c:\users\bjorn\appdata\roaming\npm\lessc
c:\users\bjorn\appdata\roaming\npm\lessc.cmd
while on my office PC, they go under program files:
(dev) go|w:\srv> which lessc
c:\program files\nodejs\lessc
c:\program files\nodejs\lessc.cmd
I need to provide the full path to a number of these global tools to PyCharm's file watcher, and since the project file i shared it would make sense to not have global resources under a user folder.
Why would the global installs end up in different folders, and how can I force them to a location that is common to all team members?
According to: https://docs.npmjs.com/files/folders
Local install (default): puts stuff in ./node_modules of the current package root.
Global install (with -g): puts stuff in /usr/local or wherever node is installed.
Install it locally if you're going to require() it.
Install it globally if you're going to run it on the command line. -> If you need both, then install it in both places, or use npm link.
prefix Configuration
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.
The docs might be a little outdated, but they explain why global installs can end up in different directories:
(dev) go|c:\srv> npm config ls -l | grep prefix
; prefix = "C:\\Program Files\\nodejs" (overridden)
prefix = "C:\\Users\\bjorn\\AppData\\Roaming\\npm"
Based on the other answers, it may seem like the override is now the default location on Windows, and that I may have installed my office version prior to this override being implemented.
This also suggests a solution for getting all team members to have globals stored in the same absolute path relative to their PC, i.e. (run as Administrator):
(Run this in cmd, not in PowerShell!)
mkdir %PROGRAMDATA%\npm
setx PATH "%PROGRAMDATA%\npm;%PATH%" /M
npm config set prefix %PROGRAMDATA%\npm
open a new cmd.exe window and reinstall all global packages.
Explanation (by lineno.):
Create a folder in a sensible location to hold the globals (Microsoft is
adamant that you shouldn't write to ProgramFiles, so %PROGRAMDATA% seems
like the next logical place.
The directory needs to be on the path, so use setx .. /M to set the
system path (under HKEY_LOCAL_MACHINE). This is what requires you to run
this in a shell with administrator permissions.
Tell npm to use this new path. (Note: folder isn't visible in %PATH% in
this shell, so you must open a new window).
These are typical npm paths if you install a package globally:
Windows XP - %USERPROFILE%\Application Data\npm\node_modules
Newer Windows Versions - %AppData%\npm\node_modules
or - %AppData%\roaming\npm\node_modules
Just press windows button and type %APPDATA% and type enter.
Above is the location where you can find \npm\node_modules folder. This is where global modules sit in your system.
As of today, global packages installed like for eg. npm i -g #vue/cli are by default store in a directory:
C:\Users\<YourUserName>\AppData\Roaming\npm\node_modules
If you're just trying to find out where npm is installing your global module (the title of this thread), look at the output when running npm install -g sample_module
$ npm install -g sample_module C:\Users\user\AppData\Roaming\npm\sample_module ->
C:\Users\user\AppData\Roaming\npm\node_modules\sample_module\bin\sample_module.js
+ sample_module#5.1.0 updated 1 package in 2.821s
here is how I install react globally on windows,
npm install -g react
and I can see package installed at,
C:\Users\username\AppData\Roaming\npm\node_modules
I install node v14.9.0 using nvm and I find it in the D drive: D:\Users\<user_name>\AppData\Roaming\nvm\v14.9.0\node64.exe.

node user directory leading to command not found

I suipidly ran this script to stop having to use sudo on npm -g commands and now my node_modules are located at:
/Users/myusername/.npm-packages/lib/node_modules
when trying to run a commands i.e yo bower i get -bash: bower: command not found
Do I need to link this directory to the usr/bin?
In trying to sort this out I have run lots of commands and created symbolic links, I feel like my system is a real mess and I cannot really see what is going on? Help to ensure my system is clean would be really helpful.
Generally, it's a good idea to ensure that you do not need admin rights to run npm commands. The problem you're having, is that the directory where npm now installs its commmands (/Users/myusername/.npm-packges/bin) is not in your PATH. Your PATH is a list of directories where your shell searches for the commands you type.
You can add the directory to your path by adding the following line to the .bashrc file in your home directory (create it if it doesn't exist).
export PATH="${PATH}:/Users/myusername/.npm-packages/bin"
Open a new terminal, and bower should work again.
As an additional tip, I believe you're running on OS X? When installing Node.js using Homebrew, it is automatically installed in a proper way (no sudo needed to install packages globally and they will just work). Next time you're installing Node.js (or something else), Homebrew might be worth a try.

Resources