node in ubuntu: can't run without full path despite $PATH setting - node.js

A seemingly contradictory set of outcomes:
$ /usr/local/bin/node --version
v5.2.0
$ node
The program 'nodejs' is currently not installed. You can install it by typing:
sudo apt-get install nodejs
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
I would appreciate if someone could point out what could possibly be wrong with the configuration of the $PATH.
For installing node, I've followed these instructions.
Thanks.

Despite source ~/.bashrc the PATH was not being updated. Yet, upon creating a new terminal with a new bash prompt, then it did: which node showed the correct path, and node resulted in a prompt. Puzzling. But resolved.

Related

sudo: npm: command not found in WSL

I'm trying to install globally some packages for my Unix environment on Windows with WSL. I use nvm to manage the different versions of Node.js.
The problem is while using the sudo command before a global npm install :
sudo npm install --global prompt-pure
I get an error: sudo: npm: command not found !
Doing a simple npm install --global pure-prompt will work, but as I'm not super user, the global installation ends up with a permission error.
How can I fix this annoying problem and keep nvm ?
Thanks by advance
When you try to run sudo npm, it tries to run the npm binary file /usr/bin/npm but your binary is located in a different place, which can be found running which npm.
Example: /home/damo/.nvm/versions/node/v8.11.1/bin/npm
The solution is to create a link in /usr/bin/ pointing to the actual binary:
sudo ln -s "$(which npm)" /usr/bin/npm
You can also add the following link so you can run sudo node
sudo ln -s "$(which node)" /usr/bin/node
For me I needed to actually cheat and run as root before installing node as root. For this I ran sudo su in a Ubuntu WSL term and then installed node.
Once I did that I could sudo su then npm run special-script.
I don't know a better way to get a script to attach to restricted ports like 443 for testing https connections but it works.
As you will find in the man file for sudo man sudo sudo will execute a command as another user. That other user has a different home directory to you and access to different commands
When i run sudo which node i get nothing, but which node returns /home/damo/.nvm/versions/node/v8.11.1/bin/node
Lets look at your actual goal. You say you are trying to install pure-prompt, i know this does not ask your explicit question but given you have zsh installed have you tried oh-my-zsh (https://github.com/robbyrussell/oh-my-zsh) i use this on every install of linux i ever have to work with (VMs, WSL, docker). Very customizable and looks great out of the box.

Running node in command line w/o sudo

I installed NodeJS on my Ubuntu server and now I'm wondering why I can't use it by simply typing "node". "nodejs" works fine, but only "node" should work, too, right?
"sudo node" works as well, so I thought this might be a problem with the PATH, but the PATH variables are identical:
root#myserver:/# node -v
-bash: /usr/sbin/node: No such file or directory
root#myserver:/# sudo node -v
v8.2.1
root#myserver:/# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
root#myserver:/# sudo echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
What am I doing wrong here? Thx.
I have had this issue in the past. I started installing Node like this on Ubuntu I have never had any issues with just using the node command, it has just worked:
rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
Let me know if this works. You can read more about it here: https://nodejs.org/en/download/package-manager/
Did you install using apt-get? This is not recommended. Remove nodejs package and install from nodejs.org, https://nodejs.org/en/download/current/
You might also want to consider using something like nvm to manage your node versions, https://github.com/creationix/nvm
Using nvm you can solve this issue, and as a plus, it gives you the ability to install multiple node versions and bind the node command to whichever version you prefer
After installing nvm:
nvm install vX.Y.Z
nvm alias default vX.Y.Z
node -v // vX.Y.Z

Issues installing Node via homebrew

I'm attempting to get the LTS version of node installed on my machine via homebrew but I seem to be having some issues. If I run brew install node#6 to get the latest LTS version (6.10.3), everything seems to complete with no issues. Then, if I run brew list, I will see node#6 has indeed installed. BUT, if I check which version of node is installed using node -v, I receive zsh: command not found: node. I'm pretty new to homebrew and the command line in general. Where have I gone wrong?
Any help would be greatly appreciated, and I can provide more info if requested. Thanks!
For those curious, I ended up just using brew install node to install the most current version of node. Trying to install node#6 was troublesome, but it seems my struggle was pertaining to an issue where homebrew required node#6 to be linked using brew link node#6 --force. More info can be found here:
https://github.com/Homebrew/brew/issues/2220
Note that I did not try this solution.
As indicated in the GitHub issue linked above, if you run brew info node#10 (replace node#10 with your version) it provides a line to add the non-latest node version to your path:
If you need to have node#10 first in your PATH run:
echo 'export PATH="/usr/local/opt/node#10/bin:$PATH"' >> ~/.zshrc
Running echo 'export PATH="/usr/local/opt/node#10/bin:$PATH"' >> ~/.zshrc adds a line to the bottom of my .zshrc file:
export PATH="/usr/local/opt/node#10/bin:$PATH"
If you're using Bash, it'd be your .bashrc file. I'm guessing Homebrew picks up on that, but worth double checking.
This line will add /usr/local/opt/node#10/bin to my PATH when I start my terminal. I need to restart my terminal to get immediate access. Or I can re-source the .zshrc file. I only have to do this the first time:
source ~/.zshrc
Now node works. I can verify by checking the version:
node -v
# v10.17.0
Could you check if /usr/local/bin is in your $PATH? Also, maybe run brew doctor?

Update Node.js: Error while updating Node.js on Mac

I am using mac. I have currently installed Node.js 4.4.3
Aleeshas-MacBook-Air:~ aleesha$ node -v
v4.4.3
I want to update Node.js to it's latest version by following these steps.
First I tried to execute: brew update. However, I was getting the below error when I executed the command:
brew update Error: /usr/local is not writable. You should change the
ownership and permissions of /usr/local back to your user account:
sudo chown -R $(whoami) /usr/local
So after searching for solution on Google, I ran this command: sudo chown -R $(whoami) /usr/local
After that I again executed the brew update command. I think it was successfully executed since I didn't see any error message. Last few lines on the command prompt were:
==> Migrating HOMEBREW_REPOSITORY (please wait)...
==> Migrated HOMEBREW_REPOSITORY to /usr/local/Homebrew! Homebrew no longer needs to have ownership of /usr/local.
If you wish you can
return /usr/local to its default ownership with: sudo chown
root:wheel /usr/local Aleeshas-MacBook-Air:~ aleesha$
However after this step when I execute the upgrade command, it failed.
Aleeshas-MacBook-Air:~ aleesha$ brew upgrade node
Error: node not installed
Aleeshas-MacBook-Air:~ aleesha$
I am not sure what exactly needs to be done here.
Thanks
EDIT: Executed brew doctor to check for system anomalies.
Aleeshas-MacBook-Air:~ aleesha$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!
Warning: "config" scripts exist outside your system or Homebrew directories.
`./configure` scripts often look for *-config scripts to determine if
software packages are installed, and what additional flags to use when
compiling and linking.
Having additional scripts in your path can confuse software installed via
Homebrew if the config script overrides a system or Homebrew provided
script of the same name. We found the following "config" scripts:
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3-config
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5-config
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5m-config
Warning: No developer tools installed.
Install the Command Line Tools:
xcode-select --install
Warning: Python is installed at /Library/Frameworks/Python.framework
Homebrew only supports building against the System-provided Python or a
brewed Python. In particular, Pythons installed to /Library can interfere
with other software installs.
Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.
Unexpected header files:
/usr/local/include/node/android-ifaddrs.h
/usr/local/include/node/ares.h
/usr/local/include/node/ares_version.h
/usr/local/include/node/libplatform/libplatform.h
/usr/local/include/node/nameser.h
/usr/local/include/node/node.h
/usr/local/include/node/node_buffer.h
You didn't install Node using Homebrew, use this gist to uninstall your current installation of Node.
And then:
brew update && brew install node
It seems like you didn't install nodejs through brew. If you installed Node without brew, brew doesn't recognize it. I had the same issue until I realised that I installed Node with Node Version Manager. Make sure you installed it using Brew.
brew list
Will show you the things brew installed.

casperjs command not found

I installed phantomjs on my mac
phantomjs --version
1.9.2
Following the instructions on the casper installation page, I did this
$ git clone git://github.com/n1k0/casperjs.git
$ cd casperjs
$ ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs
However, i'm getting command not found when I run casperjs. I also installed it via homebrew and got the same result.
Any suggestions?
Update
this is what I get when I type $PATH
-bash: /Users/me/.rbenv/shims:/opt/local/bin:/opt/local/sbin:/Users/me/downloads/mongodb-osx-x86_64-2.4.5/bin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/MacGPG2/bin: No such file or directory
inside
usr/local/bin, there is (among other things) phantomjs and casperjs. I can run phantomjs --version from anywhere and get the version number. casperjs doesn't work
This is most likely due to the symbolic link not being created.
Try running the command directly: /usr/local/bin/casperjs --version
If this results in another Command not found error, then you should try running
sudo ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs
to make sure that you actually create the link in the /usr/local/bin directory
However, if it does exist, you should check to make sure that the link is pointing to the correct directory by running
ls -l /usr/local/bin/casperjs
It seems that you got the ln command line parameters switched: first one is the target, second is the link.
Install casperjs using npm
npm install casperjs
Then you can run it using:
node_modules/casperjs/bin/casperjs selftest

Resources