NodeJS By Examples

NodeJS Installation on CentOS 6.5 - Compiling NodeJS from the latest source

Installation - NodeJS on Centos 6.5

1. yum list nodejs

nodejs.i686

nodejs.x86_64

 

2. yum install nodejs    -install nodejs.x86_64

 

Now if we run:

#node --version

v0.10.33

 

But #npm --version  return error. This error means that NPM is not installed and we have to install it separatelly from NodeJS.

I don't like it because in Ubuntu when we install NodeJS, NPM is installed automatically. In CentOS this is not the case.

So let's try to compile NodeJS from the source.

 

Compiling NodeJS from the source

0. Check Node distribution: http://nodejs.org/dist/

1. # cd /usr/src

2. Grab the latest:  # wget http://nodejs.org/dist/node-latest.tar.gz

3. Untar:   # tar tar zxf node-latest.tar.gz

4. # cd node-v0.10.36

5. Prepare compiler commands: # ./configure

6. Compiling with command:  #make (this takes about 6 minutes to compile everything)

7. #make install    This puts compiled binaries into the system path, for example /usr/bin/node so we can use #node command.

 

8.Installed versions and checks

#/usr/local/bin/node -v     returns v0.10.36

#/usr/local/bin/npm     returns 1.4.28

#/usr/local/bin/npm list   is empty now

/usr/src/node-v0.10.36
└── (empty)

 

9. Create symbolic links

# ln -s /usr/local/bin/node /usr/bin/node

# ln -s /usr/local/bin/npm /usr/bin/npm

Now we can use simple #node and #npm short commands.

 

INSTALLATION DIRECTORY PATHS

NodeJS is installed into:

/usr/local/include/node/    - node API javascript library

/usr/local/bin/node      - node executable binary file

 

NPM is installed into:

/usr/local/lib/node_modules/         - node modules

/usr/local/bin/npm          -npm binary file

 

Now when we install module globally it will go into /usr/local/lib/node_modules/ for example  # npm install -g express .

 

Sometimes it is needed to add environment variable:

# export NODE_PATH=/usr/local/lib/node_modules

Then chechk with command

#printenv

 

If you want to permanently set env variable on Centos 6.6 do this:

#vim /etc/profile.d/node.sh

export NODE_PATH=/usr/local/lib/node_modules

Now, env variable will be set after server reboot.