Node

From eBabel wiki
Jump to: navigation, search

Install latest (6.x) Node.js on Ubuntu 16.04

cd ~
curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt-get install nodejs
sudo apt-get install build-essential

You can then check which version of node 6.x and npm you installed:

node -v
npm -v

Install old Node.js on Ubuntu 16.04

Source: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm

At the time of writing, this installs node.js 4.6.2 on Ubuntu 16.04

This is an older version and may not be suitable.

Error: Cannot find module 'github-url-from-git'

To list all the currently globally installed node modules:

ls /usr/local/lib/node_modules/|sort

For example, I have:

npm
rimraf
grunt-cli
gulp-cli
bower
eslint
babel-eslint
react-native-cli
eslint-plugin-react
eslint-plugin-import
eslint-plugin-jsx-a11y
eslint-plugin-react-native
eslint-config-airbnb

To solve the issue, start by deleting all installed node modules:

sudo rimraf /usr/local/lib/node_modules/

If you don't have rimraf installed you can use:

sudo rm -rf /usr/local/lib/node_modules/*

Then reinstall Node.js and npm from https://nodejs.org/en/ - Node is still there but npm had just been deleted.

After re-installing npm, you should re-install all your global dependencies that you deleted.


Deploy a nodejs app on an Amazon EC2 instance

Install nodejs on Ubuntu 12.04

 sudo apt-get install python-software-properties
 sudo apt-add-repository ppa:chris-lea/node.js
 sudo apt-get update
 sudo apt-get install nodejs

Source: https://rtcamp.com/tutorials/nodejs/node-js-npm-install-ubuntu/

Note alternatively for Centos / RHEL package management

 curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -
 sudo yum install -y nodejs
 node --version
 npm --version

Source: https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora

Port forwarding 8080 to 80 on NodeJS only instance

Note: this is not for instances that run both nodejs and Apache. See further for that special case.

 sudo vim /etc/sysctl.conf

Uncomment line "net.ipv4.ip_forward"

 sudo sysctl -p /etc/sysctl.conf
 cat /proc/sys/net/ipv4/ip_forward
 sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
 sudo iptables -A INPUT -p tcp -m tcp --sport 80 -j ACCEPT
 sudo iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT

Source: http://www.lauradhamilton.com/how-to-set-up-a-nodejs-web-server-on-amazon-ec2

Port forwarding 8080 to 80 on instance running both NodeJS and Apache

Enable the proxy modules:

 sudo a2enmod proxy_http

In /etc/apache2/sites-enabled/000-default

 <VirtualHost *:80>
   ServerAdmin hello@ebabel.eu
   ServerName ebabel.eu
   ServerAlias www.ebabel.eu

   ProxyRequests off

   <Proxy *>
       Order deny,allow
       Allow from all
   </Proxy>

   <Location />
       ProxyPass http://localhost:8080/
       ProxyPassReverse http://localhost:8080/
   </Location>
 </VirtualHost>

Source: http://thatextramile.be/blog/2012/01/hosting-a-node-js-site-through-apache


Write a script that will run the nodejs app in the background

Go to the root of your app.

 touch start_node.sh
 vi start_node.sh

Insert just one line:

 node [full path to the js that will run the app] >> /tmp/nameofyoursite.log &

For example:

 node /home/myusername/anotherdirectory/server.js >> /tmp/nameofyoursite.log &

To run this .sh script:

 ./start_node.sh

Misc. notes

How to prevent click jacking

This can be simple done via setting the http header X-Frame-Options:

Nodejs/clickjacking