Node.js stops working when I log out - node.js

I logged in as root to my CentOS 5/cPanel server and I typed the following:
cd /usr/local/bin/
git clone --depth 1 http://github.com/joyent/node.git
cd node
git checkout origin/v0.4 # optional. Note that master is unstable.
export JOBS=2 # optional, sets number of parallel commands.
mkdir ~/local
./configure --prefix=$HOME/local/node
make
make install
echo 'export PATH=$HOME/local/node/bin:$PATH' >> ~/.profile
source ~/.profile
It seems to be working fine until I log out from the server and log back in it's as it wasn't installed:
[~]# node test.js
-bash: node: command not found
If I type: source ~/.profile it starts working again until I log out.
Please help. Thanks.
EDIT:
This is the content of my .bash_profile, how should I change it?
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
unset USERNAME

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
Maybe you have a .bash_profile and it's being used instead?

Related

I am trying to use a bash script to set my paths in linux but it is not working

I have this bash script but it wont set the paths and I am not sure why. I can set the paths manually by typing each command.
#!/bin/bash
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
export PATH=$PATH:$parent_path/arm-2013.11/bin;
export ARMGCC_INSTALL_PATH=$parent_path/arm-2013.11;
export ARMGCC_VERSION=4.8.1;
You should use the source/. built-in to run this script.
Running vs Sourcing the script:
Running the script opens a new shell. All changes to the environment will be lost after the program executes.
Sourcing the script runs the script in the current shell. Think of .bashrc - it is being sourced, therefore allowing changes to the environment.
So you should run:
source /path/to/script.sh
Speaking of .bashrc, if you find the process of sourcing the script tedious, you should add this to your .bashrc:
alias aliasnamehere="source /path/to/script.sh"
and change the alias to the one you prefer.

Moving PATH to my .bashrc?

Following this guide,
I don't get the last line :
Don't forget to add the PATH to your .bashrc to make it permanent.
What exactly should I be doing ?
The statement you quoted is about the export PATH=$PATH:/usr/local/bin line from
For testing add /usr/local/bin to your path
export PATH=$PATH:/usr/local/bin
node -v
npm -v
The fragment PATH=$PATH:/usr/local/bin adds the path /usr/local/bin to the content of the PATH environment variable. This variable is used by the shell (bash) to search for executable programs.
The export in front of it tells bash to forward the updated PATH variable to all programs it launches.
Adding the line to .bashrc makes it run again every time you start a new bash instance (the environment variables are not stored anywhere, their values are set when bash starts).
/usr/local/bin is a standard path. Most probably you don't have to add that line to .bashrc or execute it at all. Use echo $PATH to check. If its output already contains /usr/local/bin (or contains it twice if you already executed the line above) then you don't need to do anything else. bash is able to find node and npm.
If /usr/local/bin is not in the output of echo $PATH (or it is only once after you have executed export PATH=$PATH:/usr/local/bin then run:
echo 'export PATH=$PATH:/usr/local/bin' >> ~/.bashrc
to put export PATH=$PATH:/usr/local/bin on a new line at the end of ~/.bashrc.

bash changing directory when started

I have both Bash on Ubuntu on Windows and Cygwin bash installed on my machine, and both are setup to have the same ~ folder (via /mnt/c/source and /cygdrive/c/source respectively).
When I start Ubuntu's bash prompt via bash --login -i (or just bash --login) from any directory, I get a prompt running from within that directory; however, when I start Cygwin's bash via the same command, the current directory is overridden, and the prompt is always at ~. See the screenshots for a simple example.
My user directory's .bashrc and .bash_profile are of course the same, as both are using the same user directory. I've looked into Cygwin's /etc/bash.bashrc and there doesn't seem to be anything there to change my current directory, and there aren't any other relevant files in /etc.
What could be causing Cygwin's bash to change directory?
you just add a command "cd /dir_you_want" at the bottom of ~/.bashrc in cygwin
I've figured it out, so in case anyone runs into the same issue:
There's one file I neglected to look into, because I didn't know it exists, /etc/profile. In Cygwin, by default it has the following section in it:
# Make sure we start in home unless invoked by CHERE
if [ ! -z "${CHERE_INVOKING}" ]; then
unset CHERE_INVOKING
else
cd "${HOME}" || echo "WARNING: Failed attempt to cd into ${HOME}!"
fi
Disabling that solves the issue of course.

Closing the terminal doesnt remember my .profile changes for nvm/npm

I installed nvm/npm using this instruction
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-an-ubuntu-14-04-server (How To Install Using NVM), but everytime I close my terminal, it seems to forget all the settings and I have to do the command:
source ~/.profile then select the npm version to make it run again. How can I keep my settings permanent, or at least for the duration of my logged in session? Thanks! (linuxmint 17)
Sourcing ~/.profile
~/.profile is typically invoked from a login shell. See http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files
Invoked as an interactive login shell, or with --login
When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
Opening a terminal does not typically invoke a login shell. Have you tried rebooting/relogging? Additionally if either ~/.bash_profile or ~/.bash_login exist, then bash won't source ~/.profile. There are two common ways to fix this:
Option 1
Move commands from the ~/.profile file to ~/.bash_profile
Option 2
Source ~/.profile from ~/.bash_profile:
[[ -f ~/.profile ]] && . ~/.profile
Default Node version
You can configure a default node version by aliasing it to default
To set a default Node version to be used in any new shell, use the alias 'default':
nvm alias default node
See https://github.com/creationix/nvm

Bash: Invalidated commands

I've incurred a worrisome issue with my bash shell. I was editing my bash_profile and accidentally exported an incomplete command (export PATH=/usr/local/bin). After I had reloaded my terminal, nearly all of my bash commands fail to work properly. When I try to run any one of them, the errors state: command not found.
How do I fix this? Is there an alternate way to open or find my bash_profile?
I would appreciate any immediate input I can get on this issue. Thank you in advance.
You can execute commands if you can give the directory name. Almost all the basic Unix commands are under the /bin or /usr/bin directory. For example, /bin/mv.
Fortunately, builtin commands are still recognizable.
Move your .bash_profile and .bashrc file out of the way for now, and see what the system default is.
You can manually edit your PATH on the command line to:
$ PATH="/bin:/usr/bin"
$ cd
$ mv .bash_profile .bash_profile.bak
$ mv .bashrc .bashrc.bak
$ mv .profile .profile.bak
$ mv .bash_login .bash_login.bak
NOTE: Some of these mv command may fail simply because that particular file may not exist.
which will give you access to most of the basic Unix commands. Or you can specify the commands with their full directory names:
$ PATH="/bin:/usr/bin"
$ cd
$ /bin/mv .bash_profile .bash_profile.bak
$ /bin/mv .bashrc .bashrc.bak
$ /bin/mv .profile .profile.bak
$ /bin/mv .bash_login .bash_login.bak
Now, log in again and see what your default $PATH is set to. This is set by the /etc/profile. You might find that's just fine, and remove setting PATH in your startup script.
The standard for PATH is something like this:
/usr/share/bin or /usr/local/bin - These contain non-standard Unix/Linux commands. For example, if you install Maven on your system, the mvn command will usually be located in one of these directories (maybe as a symbolic link). This directory is a place where commands not found in the /bin and /usr/bin directory are stored. This directory is first, so you can replace the version which came with your system with more recent versions. For example, I might have VIM 6.4 installed, but I want to use version 7.3 instead.
/bin:/usr/bin - The standard directories where 99% of the Unix commands live.
$HOME/bin - These are executables you wrote -- either scripts or binaries. This is at the end of the PATH list because it makes sure that you don't accidentally execute the wrong version of the command. Imagine if some joker wrote a shell script called cp that executed /bin/rm instead and placed it in your $HOME/bin directory.
Other directories you'll see may include /sbin/ and /usr/sbin which are administrator commands (ping and ifconfig are sometimes in one of these directories.) /opt/bin/X11 (or wherever the X11 binaries are stored). Sometimes other commands will futz around with your PATH, for example Perlbrew.
#fedorqui's comment provides a quick fix.
The OP could also have used the following to quickly get to a shell with default values for $PATH:
To create a bash shell with a pristine default environment:
without running profile/initialization scripts
without inheriting any environment variables from the current shell
run:
/usr/bin/env -i bash --norc
Note:
Due to use of env's -i option, many environment variables that are normally set will NOT be set in the resulting shell , such as USER, HOME and LANG.
Similarly, the $PATH value you'll get is presumably one hard-coded into bash itself, but it should provide access to at least the standard utilities.
--norc suppresses loading of ~/.bashrc, which normally happens by default for interactive non-login bash shells (bash also supports the --noprofile option to suppress loading of /etc/profile and ~/.bash_profile, but it doesn't apply here, since the shell created is a non-login shell).
If env is in the current shell's $PATH, env -i bash --norc will do.
env is in /usr/bin/ on at least Linux and on FreeBSD/OSX, probably also on other platforms.

Resources