Node.js

From GeilThings

Jump to: navigation, search
Node.js
Nodejs-green.png
General
Version 0.10.15
Stable 0.10.15
Function Programming Language
Package Manager npm
Scripts
Version Script node -v

Contents

General

Node.js is sponsored by Joyent, Inc.

Logos: http://nodejs.org/logos/

Interesting links:

The Node Beginner Book (really good): http://www.nodebeginner.org/
http://ronenagranat.blogspot.com/2011/02/apache2-reverse-proxy-for-nodejs.html
http://wavded.tumblr.com/post/475957278/hosting-nodejs-apps-on-centos-5
http://dailyjs.com/2010/03/15/hosting-nodejs-apps/
http://arguments.callee.info/2010/04/20/running-apache-and-node-js-together/
Mixu's tech blog - Learning node.js: my experiences and helpful resources: http://bit.ly/KUPNyt
Mixu's tech blog - Essential Node.js patterns and snippets: http://bit.ly/KnL8QV
Blazing fast node.js: 10 performance tips from LinkedIn Mobile: http://bit.ly/MkJdy4
Debuggable Blog - Understanding Node.js: http://bit.ly/KUPW51
Node.js meets WebMatrix 2 (MS Azure): http://jbeckwith.com/2012/06/07/node-js-meet-webmatrix-2/
How To Node: Blog rolling with mongoDB, express and Node.js: http://howtonode.org/express-mongodb
Node.js yum package for CentOS: http://nodejs.tchol.org/

For modules and path variables:

http://nodejs.org/docs/v0.3.1/api/modules.html

Version: node -v

npm version: npm -v

Introduction to Node.js with Ryan Dahl:

http://www.youtube.com/watch?feature=player_embedded&v=jo_B4LTHi3I

Node.js Package Manager

Node.js Process Monitor

Node.js modules

# Modules useful for programming against popular databases.
 
cd /usr/local/lib
 
# node.js 0.10.0, 1: sh: node-waf: Command not found
# npm install db-mysql
 
npm install mongodb
 
npm install hiredis redis
 
npm install pg
 
# node.js 0.10.0, 1: sh: node-waf: Command not found
# npm install db-oracle
 
npm install less


Notes:

  • Cassandra: The module "cassandra" stopped working with Node.js 0.8.0.
  • MySQL: db-mysql
    • If error "Checking for library mysqlclient_r  : Missing libmysqlclient_r", then run "yum install mysql-devel"
  • PostgreSQL: pg
    • After pg 0.7.2, pg_config must be in the path, like
    • PATH=$PATH:/usr/pgsql-9.1/bin
  • Oracle XE: db-oracle
db-oracle: As of 2012 February, it does not work with Oracle XE 10g.
In case the error message "Checking for header occi.h  : Missing include files for OCI" pops up, check the prerequisites in https://github.com/mariano/node-db-oracle (...you need to have the OCI instant client libraries and include files...):
In order for the installation script to locate them properly, the OCI_INCLUDE_DIR and OCI_LIB_DIR environment variables have to be set. For example:
export OCI_INCLUDE_DIR=/opt/instantclient/sdk/include/
export OCI_LIB_DIR=/opt/instantclient
Meaning: From http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html download the files:
oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm
For installation, see http://www.oracle.com/technetwork/articles/technote-php-instant-084410.html
Then (and add the last two lines to /etc/profile):
rpm -Uvh oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
rpm -Uvh oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm
export OCI_INCLUDE_DIR=/usr/include/oracle/11.2/client64/
export OCI_LIB_DIR=/usr/lib/oracle/11.2/client64/lib/

Installation

  • CentOS 5.7
# edit /etc/profile
# add:
### NODE_PATH=/usr/local/lib/node_modules
### export NODE_PATH
# Add it also to .bash_profile from user and from root
# I also added it to /root/.bashrc
# Check that it was set
echo $NODE_PATH
 
wget http://nodejs.org/dist/node-v0.4.9.tar.gz
tar -zxvf node-v0.4.9.tar.gz
cd node-v0.4.9
# Change PYTHONHOME to default CentOS python:
export PYTHONHOME=/usr
./configure
 
# Some missing dependencies
 
## Checking for sync_file_range(2)          : no
## Checking for header port.h               : not found
## Checking for header sys/event.h          : not found
## Checking for function kqueue             : not found
## Checking for header sys/eventfd.h        : not found
 
## 'configure' finished successfully (1.108s)
 
# According to http://serverfault.com/questions/144551/node-js-configure-warnings
# some warnings should go away after installing libgnutls-dev
# I could only find gnutls-devel in yum
 
yum install gnutls-devel
 
# No help, same missings, word on the internet is that "some are platform dependent". ?
 
make
## 'build' finished successfully (2m42.128s)
make install
## 'install' finished successfully (0.086s)
 
nohup node <path_to_node_server.js> &
  • CentOS 6.2
# edit /etc/profile
# add:
### NODE_PATH=/usr/local/lib/node_modules
### export NODE_PATH
# Execute both commands in the console as well.
 
yum install gcc gcc-c++
yum install openssl-devel
 
cd /opt
wget http://nodejs.org/dist/v${NEWVERSION}/node-v${NEWVERSION}.tar.gz
tar -zxvf node-v${NEWVERSION}.tar.gz
cd /opt/node-v${NEWVERSION}
 
./configure
make
make install

Upgrade Node.js

Started with Node.js 0.4.9.
Current upgrade: tested with CentOS 6.4.
######### START EDIT #########
 
APPNAME=node.js
DATE=`date +%Y-%m-%d`
# The directory where I want to download the files.
DOWNLOADDIRECTORY=/opt
# Only relevant if Python != 2.6, 2.7.
PYTHON2BIN=/path/to/python/2.6or2.7/binary
# Only relevant if Python != 2.6, 2.7.
PYTHON2HOME=/path/to/python/2.6or2.7/home
 
######### END OF EDIT #########
 
RAWPYTHONVERSION=`python -V 2>&1`
PYTHONVERSION=${RAWPYTHONVERSION:7}
echo $PYTHONVERSION
PYTHONMAJORVERSION=${PYTHONVERSION:0:3}
echo ${PYTHONMAJORVERSION}
# python 2.4 (CentOS 5.x) stopped working somewhere around node.js 0.6.11.
if [[ ${PYTHONMAJORVERSION} < '2.6' ]] || [[ ${PYTHONMAJORVERSION} > '2.9' ]]; then
  echo "Python ${PYTHONVERSION} not supported. Node.js requires Python 2.6 or 2.7."
  export PYTHON=${PYTHON2BIN}
  export PYTHONHOME=${PYTHON2HOME}
fi
 
RAWOLDVERSION=`node -v`
OLDVERSION=${RAWOLDVERSION:1}
echo $OLDVERSION
 
RAWNEWVERSION=`curl -sd "action=ask&query=[[Node.js]]|%3FStable&format=json" http://www.geilthings.com/api.php` 
# Get Version as Substring.  
# Using grep to sed to extract STRING between first occurrence of MATCH1 and next occurrence of MATCH2:  
# http://stackoverflow.com/questions/4392106/sed-extract-string-between-first-occurrence-of-match1-and-next-occurrence-of-m  
NEWVERSION=`echo "$RAWNEWVERSION" | grep -Po '^.*?\K(?<=Stable\":\[\").*?(?=\")'`  
echo $NEWVERSION
 
echo ${APPNAME}: installed: ${OLDVERSION}, actual: ${NEWVERSION}
if [[ ${OLDVERSION} != ${NEWVERSION} ]]; then
  echo "You could update ${APPNAME} to ${NEWVERSION}"
  echo "Would you like to install ${APPNAME} ${NEWVERSION} y/n?"
  read item
  if [[ "$item" == "y" || "$item" == "Y" ]]; then
    echo "Installing ${APPNAME} ${NEWVERSION}. Follow instructions."
  else
    echo "Exit"
  fi
else
  echo "You don't need to update ${APPNAME}; it is uptodate (${NEWVERSION})"
fi
 
cd ${DOWNLOADDIRECTORY}
# wget http://nodejs.org/dist/node-v0.4.12.tar.gz
wget http://nodejs.org/dist/v${NEWVERSION}/node-v${NEWVERSION}.tar.gz
tar -zxvf node-v${NEWVERSION}.tar.gz
 
# Stop servers running node.js, maybe with kill:
ps aux | grep node
# stop service or kill -9 <node_processID> or whatever
# ...
 
# Uninstall.
 
# First remove old npm packages.
rm -rf /root/.npm
 
cd /opt/node-v${OLDVERSION}
make uninstall
 
cd /opt/node-v${NEWVERSION}
 
# +++ See below for node.js 0.8.2, the flags CFLAGS+=-O2 CXXFLAGS+=-O2 must be used under CentOS 6.3 +++
# +++ See below for node.js 0.6.7 and CentOS 5.7 patch +++.
./configure
make
make install
 
# Test.
# node.js version: 
node -v
# npm version: 
npm -v
 
# Install npm modules again.
# ...
 
# Test applications.
# ...
 
# Move old node.js and delete it  some time later.
mv /opt/node-v${OLDVERSION} /opt/old_node-v${OLDVERSION}

Errors

node 0.8.13, npm 1.1.65, CentOS 6.3

See https://github.com/isaacs/npm/issues/2907.

npm install whatever
# npm ERR! cb() never called!
# npm ERR! not ok code 0

node.js 0.8.2 could not be compiled under CentOS 6.3

./configure
make CFLAGS+=-O2 CXXFLAGS+=-O2
make install CFLAGS+=-O2 CXXFLAGS+=-O2

node.js 0.6.7 could not be built (make fails)

  • Centos 5.7:
    • Kernel: 2.6.18-274.12.1.el5, January 2012, after fresh yum update.
    • glibc: 2.5
  • CentOS 6.2:
    • Kernel: 2.6.32-220.2.1.el6.x86_64, January 2012, after fresh yum update.
    • glibc: 2.12

According to http://lists.ofono.org/pipermail/ofono/2009-August/000339.html, ..."SOCK_NONBLOCK and SOCK_CLOEXEC are only available on kernel >= 2.6.28 & glibc >= 2.9"...

src/unix/core.c: In function uv__accept:
src/unix/core.c:739: error: SOCK_NONBLOCK undeclared (first use in this function)
src/unix/core.c:739: error: (Each undeclared identifier is reported only once
src/unix/core.c:739: error: for each function it appears in.)
src/unix/core.c:739: error: âSOCK_CLOEXECâ undeclared (first use in this function)
make[1]: *** [src/unix/core.o] Error 1
make[1]: Leaving directory `/opt/node-v0.6.7/out/Release/deps/uv'
Waf: Leaving directory `/opt/node-v0.6.7/out'
Build failed:  -> task failed (err #2):
        {task: uv uv.h -> uv.a}
make: *** [program] Error 1

Other references to SOCK_CLOEXEC & Co.:

Obsolete

  • Remove manually some files after uninstall
This is not necessary anymore after 0.8.0.
Find remaining node files/directories and remove them. As of 0.6.6 to 17:
rm -rf /usr/local/include/node
rm -rf /usr/local/lib/node
rm -rf /usr/local/lib/node_modules

  • Missing dependencies for 0.4.9, 0.4.12: They don't show up anymore for node.js 0.6.3 and up.
Checking for sync_file_range(2)          : no
Checking for header port.h               : not found
Checking for header sys/event.h          : not found
Checking for function kqueue             : not found
Checking for header sys/eventfd.h        : not found
  • Update npm itself
This is not needed anymore; since at least node 0.6.4 node includes npm.
cd /usr/local/lib
curl http://npmjs.org/install.sh | sh
cd /usr/local/lib
npm update
  • Uninstall npm modules
# As of 0.6.6 to 19, 0.8.0 to 0.8.3
cd /usr/local/lib
npm ls
# Remove all modules installed externally with npm rm <module_name>
# ...

Programming


Versions

Software name Version number Version date
Node.js 0.8.16
0.8.15
0.8.17
0.8.18
0.8.19
0.8.20
0.8.21
0.8.22
0.10.0
0.10.1
0.4.12
0.10.2
0.10.3
0.10.5
0.10.12
0.10.15
14 December 2012
1 December 2012
11 January 2013
19 January 2013
7 February 2013
15 February 2013
28 February 2013
8 March 2013
12 March 2013
22 March 2013
16 September 2011
29 March 2013
6 April 2013
27 April 2013
22 June 2013
28 July 2013

Comments

blog comments powered by Disqus