I installed Raspbian Jessie which comes with node v0.X which is the latest version of node that is available on Raspbian repositories. So I made a script which installs v6.7.0:
#!/bin/bash
sudo apt-get remove nodejs node
if [ ! $(command -v node) ]; then
mkdir -p ~/tmp
pushd ~/tmp
if [ ! -d node-v6.7.0-linux-armv6l ]; then
if [ ! -f node-v6.7.0-linux-armv6l.tar.xz ]; then
wget https://nodejs.org/dist/v6.7.0/node-v6.7.0-linux-armv6l.tar.xz
fi
tar -xJf node-v6.7.0-linux-armv6l.tar.xz
fi
cp -R node-v6.7.0-linux-armv6l/* /usr/local/
popd
fi
But when I run node -v I get this:
-bash: /usr/bin/node: No such file or directory
And when I run which node I get no output. - And the script never executes the if branch, so node is not downloaded and nothing is put into /usr/local.
If I uninstalled nodejs using aptitude, why it it still looking in /usr/bin/node?
I have checked the PATH variable, it has both /usr/local/bin and /usr/local/sbin.
How can I modify the script to work as expected?
-
Did you run node in the same shell before uninstalling it?user2313067– user23130672016年11月22日 16:03:10 +00:00Commented Nov 22, 2016 at 16:03
2 Answers 2
There may be a symlink /usr/bin/node pointing to some file that's missing. Check it with ls -alp /usr/bin/node. There can be a problem with /etc/alternatives etc.
You don't need to remove any package installed with apt to install a new version of Node.
If you want to install Node in a way that works, see my tutorial at:
which incidentally explains how to correctly install Node 6.7.0 - just change the version to the one you need.
Instead of using that script that you have - that can fail for several reasons - for example when you have node-v6.7.0-linux-armv6l in /tmp, or if you already have node-v6.7.0-linux-armv6l.tar.xz in /tmp, or if you have a symlink in /usr/bin etc. do it manually like I describe in the tutorial for the binary packages. Just change the file name to the version that you need - e.g. node-v6.7.0-linux-armv6l.tar.gz if you want 6.7.0 for ARM.
-
Thanks - but I really need this scripted since it is used by a different and quite involved setup script that installs an OS and system from scratch. I've had it working on an OS that didn't come with node, but it doesn't work now that the OS does come with node. (I will address possible failure scenarios once I get basics working). I've tried running
ls -alp /usr/bin/nodeas you suggested but it returnsls: cannot access /usr/bin/node: No such file or directory? I also ranls -l /etc/alternatives | grep nodeand that also returns nothing?Jayy– Jayy2016年11月22日 10:30:58 +00:00Commented Nov 22, 2016 at 10:30 -
@Jodes See the tutorial that I provided. It's a list of commands to run which is basically a script if you save it in a file. Additionally I explain what you need to take into account to run your programs with the newly installed Node versions - paths, environment variables, shebang lines etc.rsp– rsp2016年11月22日 13:05:15 +00:00Commented Nov 22, 2016 at 13:05
The problem is likely that bash had run it previously, so has a hashed version in its cache. You can read more about it here.
You can confirm this by running
type node
and if you see something like
node is hashed (/usr/bin/node)
then this is indeed the problem.
You can clear all the hashes with hash -r or just this one with hash -d node
As an aside, which is really a tool that was written for csh, and there are edge cases where it doesn't work as expected in bash. Consider using type -p and command -v to find locations with bash and get a really excellent write up on it here