-bash: /usr/local/bin/npm: No such file or directory - node.js

I was installing a boilerplate in my Mac, then it says your node version is outdated and we can't find gulp, try npm install gulp,
I put in my console
npm install -g npm; npm install gulp
and after that, it doesn't matter what I do with npm . . . in the terminal, I always get this -bash: /usr/local/bin/npm: No such file or directory
here my last logs
CRSJOPGP0223:nodeboilerplate bermarce$ npm start
-bash: /usr/local/bin/npm: No such file or directory
CRSJOPGP0223:nodeboilerplate bermarce$ npm cache clean
-bash: /usr/local/bin/npm: No such file or directory
CRSJOPGP0223:nodeboilerplate bermarce$ sudo npm install npm -g
Password:
sudo: npm: command not found
CRSJOPGP0223:nodeboilerplate bermarce$ npm -v
-bash: /usr/local/bin/npm: No such file or directory
CRSJOPGP0223:nodeboilerplate bermarce$ node -v
v4.1.0
CRSJOPGP0223:nodeboilerplate bermarce$
I have that version of node, v4.1.0, but anything with npm works.
Do you guys have an idea ?
EDIT
-bash: nvm: command not found
UPDATE
I just uninstall everything regarding node and nvm in my Mac, then install node and nvm again and it works, I do nvm and node and npm and returns some instructions so this means it is installed.
But if I open a new tab in the terminal and type the same, I got no responde
-bash: nvm: command not found <--that again
so then I installed again everything in the new tab of the terminal and it works again. Is like if its not installed globally or something.

Your problem is that nvm is not in your path. Open up your ~/.bashrc file (or ~/.zshrc if you're using zsh) and add this at the end:
. ~/.nvm/nvm.sh
Save, exit and open another terminal tab and everything should be ok!

make the directory
sudo mkdir -p /usr/local/bin
2.Create a symbolic link from the installed location to the location it is looking for
sudo ln -s /usr/bin/npm /usr/local/bin/npm

Your question is a bit old, but as other people might still stumple upon it...
The installation process of nvm should have added these lines to ~/.bashrc:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
There's no need to add . ~/.nvm/nvm.sh.

I was trying to fix some permission errors and clearly did the wrong thing, resulting in whenever i used npm it would give me:
bash: /usr/local/bin/npm: No such file or directory
I just went to node.js website and selected the installer,
ran that.
Once completed it setup all the files required.
This sorted my npm installation issues out for me.
Edit
Not quite i still needed to setup permissions to install in node, but it does setup the required directories.
I had a look at this video to see how to update my permissions cause my user didn't have access to my node_modules file
https://www.youtube.com/watch?v=bxvybxYFq2o
hope this helps you out.

The solution is as follows:
sudo apt-get install nodejs
sudo npm -v
You can see the installed npm version

Related

env: node: No such file or directory in mac

I'm searching it in google on how to show the version of node js or how I'm gonna work with npm?
If I do this
npm -v
even I already install it using brew install node. The result is always
env: node: No such file or directory
I already do a lot of command to solve for it, but failed.
these are the command that I already try:
sudo apt-get install nodejs-legacy
sudo ln -s /usr/local/bin/node /usr/bin/node
brew unlink node
brew link node
I can't start the task because of this. help me guys. This work before after I used this command brew link --overwrite node. Now I have problem, i cant work anymore with npm. I'm really new about this. I don't have any idea now on how to fix this.
Update
I already get the list using npm list command. I also get the idea here link but when I npm install inside the project. I alwayst get this "result env: node: No such file or directory",even I command npm -v but If i command it outside my project. It work the npm -v
I solved it this way:
$ brew uninstall --force node
$ brew uninstall --force npm
after it
$ brew install node
which suggested me to overwrite simlinks
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink share/doc/node/gdbinit
Target /usr/local/share/doc/node/gdbinit
already exists. You may want to remove it:
rm '/usr/local/share/doc/node/gdbinit'
To force the link and overwrite all conflicting files:
brew link --overwrite node
after executing
$ brew link --overwrite node
everything worked again.
NOTE: Only mac users!
uninstall node completely with the commands
curl -ksO https://gist.githubusercontent.com/nicerobot/2697848/raw/uninstall-node.sh
chmod +x ./uninstall-node.sh
./uninstall-node.sh
rm uninstall-node.sh
Or you could check out this website: How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)
if this doesn't work, you need to remove node via control panel or any other method. As long as it gets removed.
Install node via this website: https://nodejs.org/en/download/
If you use nvm, you can use:
nvm install node
You can already check if it works, then you don't need to take the following steps with: npm -v and then node -v
if you have nvm installed:
command -v nvm
Uninstall npm using the following command:
sudo npm uninstall npm -g
Or, if that fails, get the npm source code, and do:
sudo make uninstall
If you have nvm installed, then use: nvm uninstall npm
Install npm using the following command: npm install -g grunt
I was getting this env: node: No such file or directory error when running the job through Jenkins.
What I did to fix it - added export PATH="$PATH:"/usr/local/bin/ at the beginning of the script that Jenkins job executes.
If you're switching from bash to oh my zsh, you will need to add nvm's path into the zshrc file.
#Zgpeace's answer here helped solve my issue (on IOS):
Open the .zshrc file. I used nano: nano ~/.zshrc
Add this into the file: export NVM_DIR=~/.nvm
Save changes by typing Ctrl+X --> Yes.
Reload the configuration. Type source $(brew --prefix nvm)/nvm.sh
Test by typing nvm --version.
You'll now be able to see the version. For e.g: 0.37.2
Thanks to this forum.
Sharing my notes below.
macOS Monterey
version 12.2.1
Chip Apple M1
$ brew uninstall --force node
$ brew uninstall --force npm
$ brew install node
$ brew link --overwrite node
$ brew doctor
$ brew cleanup
$ nvm --version
// 0.35.3
$ nvm use stable
// Now using node v17.6.0 (npm v8.5.1)
$ node -v
// v17.6.0
$ npm -v
// 8.5.1
I got such a problem after I upgraded my node version with brew. To fix the problem
1)run $brew doctor to check out if it is successfully installed or not
2) In case you missed clearing any node-related file before, such error log might pop up:
Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built.
node
3) Now you are recommended to run brew link command to delete the original node-related files and overwrite new files - $ brew link node.
And that's it - everything works again !!!
It was nvm changing paths for me, this fixed it:
nvm use stable
I re-installed node through this link and it fixed it.
I think the issue was that I somehow got node to be in my /usr/bin instead of /usr/local/bin.
Let's see, I sorted that on a different way. in my case I had as path something like ~/.local/bin which seems that it is not the way it wants.
Try to use the full path, like /Users/tobias/.local/bin, I mean, change the PATH variable from ~/.local/bin to /Users/tobias/.local/bin or $HOME/.local/bin .
Now it works. 🙀🙀🙀
I get this error running npm start through PyCharm on Mac. For that the answer is to start PyCharm from the Terminal with open -a '/Applications/PyCharm.app/'. See https://stackoverflow.com/a/34017083/733092.
For Pycharm Professional, starting from the command line is different. Get the JetBrains Toolbox app, go to Settings, Generate Shell Script, and it will create a ~/pycharm_shell_scripts for you to run.
Working on MacOSx, maven exec not willing to execute a shell script that I could manually execute with desired result. Gave me the same error, solved it as well with
export PATH="$PATH:"/usr/local/bin/ at the beginning of the script
For mac user using the package manager homebrew:
-brew uninstall node
-brew uninstall npm
-brew doctor
-brew link node
-node -v (to check node version)
That's all you need.
For me, this error occurred when I tried to install a package with sudo npm. This fixed it:
sudo env PATH="$PATH:/usr/local/bin/" npm <rest of command>
I was using fish shell and encountered this problem on my m1 macbook air.
install nvm omf install nvm
add nvm to path by creating this file ~/.config/fish/functions/nvm.fish
I use vim so vim ~/.config/fish/functions/nvm.fish
function nvm
bass source ~/.nvm/nvm.sh --no-use ';' nvm $argv
end
install node again nvm install node
It worked for me after that, :) hopefully it does for you too
credit https://eshlox.net/2019/01/27/how-to-use-nvm-with-fish-shell
If you just installed/modified node and if intellij/webstorm or your shell is throwing this error, it's because it has not sourced the environment variables, try restarting the IDE/shell.
I also face this issue on mac. I had installed node using brew. I was able to resolve the issue by following these steps:
uninstall node using the command: brew uninstall --force node
uninstall npm using the command: brew uninstall --force npm
Then install node using the installer provided on the following link: https://nodejs.org/en/download/
Just did an update on my Mac OS which switched my .bash to /zshrc. Here is what I added to my ~/.zshrc file:
export NVM_DIR="/Users/< my name >/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # this loads nvm
Then I did a $ source ~/.zshrc and it refreshed those values.
Double checked my work with nvm --version and it was all good.
just run cmd:
% npm
before
% npx install-peerdeps --dev eslint-config-airbnb
on Mac m1 pro
Kept getting this error, found it it's because NVM wasn't using any version of node. I didn't set this but after using these commands it seemed to fix the error
nvm install lts/*
nvm use lts/*

npm command not found error but node is installed

I installed node and npm with Homebrew a while ago, they both worked fine until today when I keep running into the npm command not found error.
When is run $ whereis node, I get nothing back
When I do $ which node, I see /usr/local/bin/node
When I do $ node -v, I see v4.4.7
When I do $ whereis npm, I get nothing back
When I do $ which npm, I get nothing back
When I do $ npm -v, I see -bash: npm: command not found
I have tried
$ brew update
$ brew uninstall npm
$ brew install npm
I have also made sure that my $NODE_PATH environment variable is set:
# In ~/.bash_profile file:
export NODE_PATH="/usr/local/lib/node_modules"
I also followed these instructions from https://himanen.info/solved-npm-command-not-found/
Nothing seems to work and I keep getting npm: command not found when I run any command in any folder with npm. Any ideas? Thanks
I had the same issue, I am using a MAC.
It was a permission issue in my case, here is what I already did:
$ brew update
$ brew uninstall npm
$ brew install npm
That didn't work for me, so I tried this:
$ sudo chmod -R 777 /usr/local/lib
$ brew postinstall node
and this linked installed node with npm, when I typed:
$ npm -v
5.3.0
Now all commands followed by NPM are working fine,
like npm install
Hope this will work for all!!
Figured out the issue. So the root of the problem was that I installed npm using Homebrew and there are some issues with what goes on under the hood with Homebrew and npm.
To fix this I did the following:
rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node --without-npm
echo prefix=~/.npm-packages >> ~/.npmrc
curl -L https://www.npmjs.com/install.sh | sh
Important!
Do this in .bash_profile
export PATH="$HOME/.npm-packages/bin:$PATH"
export PATH="$HOME/.node/bin:$PATH"
Now everything works like a charm
In mac via homebrew, when you are getting error like
Error: Permission denied # dir_s_mkdir - /usr/local/lib/node_modules/npm
or mostly getting several folder permission, don't give full permission like
$ sudo chmod -R 777 /usr/local/lib
Please use as mentioned below
$ sudo chown -R $(whoami):admin /usr/local/lib/node_modules/
What it will do, simply gives the ownership to the user (linux users also can use this).
Hint: And in mac please use homebrew for installation. Advantages of homebrew you can switch between versions, easy to uninstall, you no need to run as root (sudo), like wise lots of advantages are there, as a developer its recommended to use homebrew (https://brew.sh/). And one more thing whenever you are getting some error like permission denied or something don't, give the full permission instead of using chmod use chown.
I had the same issue, I executed following command to install node and npm - it worked perfectly.
rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node

NPM not found when using NVM

I have installed node/npm using the nvm documentation.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash
Then:
nvm install node
At this point node is working but the npm command result with:
npm: command not found
How can I have npm to work correctly ?
I found out that this was a conflict with a previous versions of npm that have not been removed properly despite a apt-get remove node.
I solved it by reinstalling npm from scratch:
rm -R ~/.npm ~/.nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
nvm install node
I found the solution here.
If you run NVM-Windows, don't forget to run nvm on. (this solve the problem as title for me.)
I fixed this by doing this command:
$ command -v npm
and then reopen the shell window.
I solved it by uninstalling all problematic node versions (e.g. v14 below) and reinstalling it.
The problem:
node --version; npm --version;
v14.17.1
Command 'npm' not found, but can be installed with:
sudo apt install npm
The solution:
nvm deactivate
echo "All versions BEFORE:"
nvm_ls
# uninstall all 14.* versions
for v in $(nvm_ls 14); do nvm uninstall $v; done
echo "All versions AFTER:"
nvm_ls
# reinstall version 14
nvm install 14
# and now it has npm too
node --version; npm --version
which node; which npm
# v14.17.1
# 6.14.13
# /home/user/.nvm/versions/node/v14.17.1/bin/node
# /home/user/.nvm/versions/node/v14.17.1/bin/npm
If you use Windows OS, make sure you removed the existing nodejs and npm.
In my case, it worked well after I remove the C:/Program Files/nodejs.
Reference is here.
During nvm installation, make sure the selected path must NOT exist.
This problem especially happens in windows which happens because of missing admin rights for cmd.
If you are using Git bash
Go in installation directory e.g C:\Program Files\Git
Right click properties -> compatibility.
Tick the checkbox with label -> Run as administrator.
Run the git bash again & execute npm list and then npm use 'version_to_be_used'
Same goes for Cmd
One possible reason is the NVM symlink is invalid.
But first, check if both NVM_HOME & NVM_SYMLINK is already set in environment path.
If not, maybe some problem with your nvm installation and u might want to reinstall.
Using explorer, open the symlink folder to check if the folder is valid.Default Symlink path in Windows: C:\Program Files\nodejs. Symlink appears as a normal shortcut in Windows explorer.
If you see node files in there, then you're fine.
If the folder is invalid, delete the symlink.
Then, execute nvm ls and nvm use <desired node version>, this step will re-create the correct symlink.
Restart CMD and test nvm current, node -v, npm -v
For Windows:
nvm creating symlink from installed node path like c:\program files\node to the c:\users<your user>\AppData\nvm<node ver>
So check:
Your basic node path in the PATH variable.
Your npm is inside c:\users<your user>\AppData\nvm<node ver>\nmp and this path is also int the PATH variable.
You could also run
source ~/.bashrc
and try to run again on the same bash terminal where you downloaded the install.sh the command:
npm -v
I went through a similar issue recently and solved it by setting the npm mirror to npm_mirror https://github.com/npm/cli/archive/refs/tags/
The default npm mirror (https://github.com/npm/cli/archive) was a broken link.
so run
nvm npm_mirror https://github.com/npm/cli/archive/refs/tags/
I had the same issue while any new terminal instance started up the message 'npm not found' was shown. I noticed that I had defined (probably) custom paths to npm and node in ~/.bashrc. Deleting them (keeping the paths for nvm) resolved the problem.
This helped me: https://github.com/coreybutler/nvm-windows/issues/548#issuecomment-768297716 Adding quotes to NVM_SYMLINK environment variable: "C:\Program Files\nodejs" instead of C:\Program Files\nodejs.
Install node using node source distribution:
curl -sL https://deb.nodesource.com/setup_[version].x | bash -
apt-get install -y nodejs
[version] = the wanted version. See the repository to choose the correct: NodeSource Node.js Binary Distributions

Npm not working after Mavericks update

My npm in terminal isn't working after Mavericks update.
node app.js works well and runs my app, but when I run npm followed by anything I get -bash: npm: command not found. I know this question has been asked before here:
Global installation with npm doesn't work after Mac OS X Mavericks update
and
How do I install a module globally using npm?
and npm not working after reinstalling Mac OS X
, but none of the answers resolved my situation.
$ echo $PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/mongodb/bin
How did you install node.js ? typically it comes bundled with both node and npm, where both are in the same directory. My suggestion is to remove your current install(s) and do the following with just works.
to install nodejs and npm as yourself NOT root do these commands (OSX/linux) :
parent_dir=${HOME}/bin_xxxx # replace bin_xxx with something specific
# to node release like bin_v0.10.31
mkdir ${parent_dir}
download source from : http://nodejs.org/download/
cd node-v0.xxxx
./configure --prefix=${parent_dir}/nodejs
make -j8
make install
which puts it into dir defined by above --prefix
export PATH=${parent_dir}/nodejs/bin:$PATH
define environment variable 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=${parent_dir}/nodejs/lib/node_modules
do above AND use syntax : npm install -g some_cool_module
always use the -g for global so it gets installed into dir $NODE_PATH
and not your $PWD
nodejs install gives you npm as well :
ls -la ${parent_dir}/nodejs/bin
For Debian, after installing node do following
curl -k -O -L https://npmjs.org/install.sh
ln -s /usr/bin/nodejs /usr/bin/node
sh install.sh

NPM modules won't install globally without sudo

I have just reinstalled Ubuntu 12.04 LTS, and before anything else i did these steps:
Installed Node via package manager with the following script
sudo apt-get update
sudo apt-get install python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
Tried to install yeoman, express, n, yeoman's generators globally and all of them returned the same error
npm ERR! Error: EACCES, symlink '../lib/node_modules/n/bin/n'
npm ERR! { [Error: EACCES, symlink '../lib/node_modules/n/bin/n'] errno: 3, code: 'EACCES', path: '../lib/node_modules/n/bin/n' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! System Linux 3.8.0-29-generic
npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" "-g" "-d" "n"
npm ERR! cwd /home/heberlz
npm ERR! node -v v0.10.20
npm ERR! npm -v 1.3.11
npm ERR! path ../lib/node_modules/n/bin/n
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, symlink '../lib/node_modules/n/bin/n'
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/heberlz/npm-debug.log
npm ERR! not ok code 0
Reclaimed ownership of the following folders recursively ~/.npm, /usr/lib/node, /usr/lib/node_modules, and of the following symlinks /usr/bin/node, /usr/bin/nodejs with absolutely no success
I need to install yeoman and its generators without sudo not to be in trouble later on :(
Ubuntu 12.04 and using Chris Lea's PPA for install the following works for me:
npm config set prefix '~/.npm-packages'
and adding $HOME/.npm-packages/bin to $PATH
Append to .bashrc
export PATH="$PATH:$HOME/.npm-packages/bin"
For more see this answer from #passy
If you already have $HOME/bin in your path, a simpler solution is just ...
npm config set prefix ~
New node commands will now install into your $HOME/bin directory.
No need to change your path!
Since this discussion is really about reducing the security risks of running sudo, you should also be aware that any node app could potentially be installing an app name that does not match the registered node package name you think you're installing. So there is a security risk that an npm install will replace an existing system command or one you already have in $HOME/bin. If you're concerned, check the bin, and scripts properties in the package.json file of the app you're installing first.
In general, it's safest to:
(a) Place $HOME/bin last in your path so system commands are not superseded.
(b) don't include "." or any relative path in your $PATH so you don't accidentally run a command that happens to be in the current directory.
Reference:
package.json properties
npm install
NodeJS security vulnerabilities: nodesecurity.io.
As for October 2014:
Node.js is available from the NodeSource Debian and Ubuntu binary distributions repository.
curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y nodejs
That's it.
Outdated answer:
The fastest way without using sudo is like described here by isaac
I strongly encourage you not to do package management with sudo!
Packages can run arbitrary scripts, which makes sudoing a package
manager command as safe as a chainsaw haircut. Sure, it's fast and
definitely going to cut through any obstacles, but you might actually
want that obstacle to stay there.
I recommend doing this once instead:
sudo chown -R $USER /usr/local
EDIT:
There are certain security concerns and functionality limitations regarding changing the ownership of /usr/local to the current user:
if there is another user on the machine who could use global npm packages - do not change the ownership of /usr/local
https://apple.stackexchange.com/questions/1393/are-my-permissions-for-usr-local-correct
https://askubuntu.com/questions/261326/is-it-safe-to-chown-usr-local
Having said that, if you want to install global module without using sudo, I don't see any better solution (from pragmatic point of view) than mentioned. Security vs easy of use is very broad topic, and there is no easy answer for that - it just depends on your requirements.
The issue was i installed node using sudo, to avoid errors when installing npm modules globally one MUST NEVER install node with sudo.
My solution was to reinstall node it this way:
Download latest stable node sources from nodejs.org #in my case node-v0.10.20.tar.gz
tar -zxf node-v0.10.20.tar.gz #uncompress sources
cd node-v0.10.20 #enter uncompressed folder
sudo chown -R $USER /usr/local
./configure --prefix=/usr/local && make && make install
One thing to note is that only taking ownership of the /usr/local folder wouldn't work in my case because node installation itself was made with sudo
Last step to install yeoman: #although at yeoman.io it says that doing "npm install -g yo" already installs bower and grunt, there are some submodules of grunt that fail, so i fixed that by installing it by itself
npm install -g bower
npm install -g grunt
npm install -g yo
npm install -g generator-angular
I solved this problem with environment variable and shell alias:
export NPM_PREFIX=$HOME/node
alias npmg="npm -g --prefix $NPM_PREFIX"
For me npm did not honor the "prefix" config setting in .npmrc.
Find the path to npm's directory:
npm config get prefix
For many systems, this will be /usr/local.
Change the owner of npm's directories to the name of the current user (your username!):
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
This changes the permissions of the sub-folders used by npm and some other tools (lib/node_modules, bin, and share).
Here is the link for full details
https://docs.npmjs.com/getting-started/fixing-npm-permissions
According to this similar SO post: npm throws error without sudo
Looks like you might have an ownership issue with ~/.npm directory.
As with the answer in that one, try:
sudo chown -R `whoami` ~/.npm
If you're on a developping machine, you might be better off considering using nvm.
If not, you simply want to install using your favorite package manager.
Whatever the case may be, I'd recommend checking this answer on stackoverflow
Actually, I just changed the permission of a user folder that was owned by root:
sudo chown -R $USER ~/.config/configstore
Then I could "npm install" and "bower install" without sudo!
Worked fine!
using lubuntu 14.04.3, I tried changing ownership of .npm and npm prefix, updated my path, npm installed modules to my home directory without sudo but the path was incorrect so the modules like ember were not found, linuxbew solved the problem, quick setup guide here for node/npm
This issue and other caused by the same reason can be solved installing Node in user space.
You can do it just copying and pasting in your terminal
NODEJS_ROOT=${NODEJS_ROOT:-~/nodejs}
cd /tmp
wget -N http://nodejs.org/dist/node-latest.tar.gz && tar xzf node-latest.tar.gz
NODEJS_CURRENT=$(tar tf node-latest.tar.gz|head -1)
mkdir -p $NODEJS_ROOT/$NODEJS_CURRENT
cd $NODEJS_CURRENT
./configure --prefix=$NODEJS_ROOT/$NODEJS_CURRENT && make install
cd $NODEJS_ROOT
rm current 2> /dev/null # Removes current symbolic link, if any
ln -s $NODEJS_CURRENT current
Same commands can be launched also to get Node updated to latest version.
Don't forget to edit your environment. Only once, do
echo "export NODEJS_ROOT=$NODEJS_ROOT" >> $HOME/.bash_profile
echo 'export PATH=$NODEJS_ROOT/current/bin:$PATH' >> $HOME/.bash_profile
source $HOME/.bash_profile # reload your env, so you can use node right now
Check out this article as a reabout how to Install Node.js without sudo.
For a more general solution about this topic (i.e., install software locally) see dotsoftware.
just use nvm
you will be able to dynamically switch between different node versions and it is installed locally for your user. No sudo needed. Just make sure you have fully cleaned up the old node beforehand.
https://github.com/nvm-sh/nvm
In order to install Node.js and npm locally without having to use sudo open the terminal and type:
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
wget -c http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install
wget -c https://www.npmjs.org/install.sh | sh
Use NVM
I had the same "permission denied" issue. Instead of trying to fix the NodeJS installed from https://nodejs.org/en/ which installs into folders that are owned by root instead of $USER, I used NVM. node version manager.
Install it: run curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Install a version of node: run nvm install 16
Use it:
nvm use 16
npm install -g yarn
Everything worked fine when I used Node installed by NVM.
The best solution I found was to install Node.js from the tar package on to user home directory & link the lib folder location. Here is what you need to do
This will install Nodejs under ~/.local/ instead of the default /usr/local/
Add this to your ~/.npmrc (create the file if it doesn't exist already):
root = /home/YOUR-USERNAME/.local/lib/node_modules
binroot = /home/YOUR-USERNAME/.local/bin
manroot = /home/YOUR-USERNAME/.local/share/man
Download the Nodejs source code from nodejs.org and install it under your ~/.local tree:
tar xf node......
cd node........
./configure --prefix=~/.local
make
make install
Create ~/.node_modules symlink. (This directory will be automatically searched when you load modules using require "module" in scripts. I'm not sure why Node doesn't search ~/.local/lib/node_modules by default.)
cd
ln -s .local/lib/node_modules .node_modules
Is ~/.local/bin in your path? Type
which npm
If it says ~/.local/bin/npm, you're done.
Otherwise, do this...
export PATH=$HOME/.local/bin:$PATH
...and add that line to your ~/.profile file, so it'll run every time you log in.
If you still encounter ownership or permission error while installing packages, then change ownership of ~/.local/ dir by running
chown -R user:user ~/.local/
Now you should be good to install packages via 'npm'
Note: ALL OF THE ABOVE COMMANDS ARE TO BE RUN AS USER. DO NOT USE SUDO OR ROOT LOGIN
NEVER EVER CHANGE THE PERMISSION OF FOLDERS UNDER '/USR/LIB/'. WILL LEAD TO UNSTABLE OS
I find Pawel Grzybek's explanations very convincing: They boil down to 3 simple sudo commands, never having to use sudo again for global npm installs:
sudo chown -R $(whoami) /usr/local/lib/node_modules
sudo chown -R $(whoami) /usr/local/bin
sudo chown -R $(whoami) /usr/local/share

Resources