The following steps are used to install a node.js backend server on a Linux EC2 Instance.
NOTE: with AWS EC2 Linux I prefer to do the install with root so first do a SUDO -i to get to root use mode. Then after the install is done you can copy the changes in the .profilerc from the root user to the ec2-user to make nvm and node commands available to both environments.
- Install the Node Version Manager (nvm) from https://github.com/creationix/nvm.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
- To download, compile, and install the latest release of node, do this:
nvm install node
Then in any new shell just use the installed version:
nvm use node
- Now go to Sublime text editor and create an hello.js file with:
// hello.js console.log('hello world from Node.js')
- Upload the Hello World server node.js to a directory on the server. I created a directory called /node
- Now you can run your node.js Hello World test
[ec2-user@ip-172-31-24-194 node.js]$ cd /node [ec2-user@ip-172-31-24-194 node.js]$ node hello.js hello world from Node.js
- Finally, you should update npm (Node Package Manager) that comes with node to the latest version. Node Package Manager works with packages available at http://npmjs.com. More on the NPM in a later post.
npm install npm@latest -g # FYI - use the next comand to check the current verion of npm npm -v
That’s All ! node.js server is setup.
You can use the which node command to find your current node.js version
[ec2-user@ip-172-31-59-183 node.js]$ which node ~/.nvm/versions/node/v7.4.0/bin/node
I also install the latest nightly build of node with the following command. You will then have the latest LTS version plus the nightly build and can switch between them with the NVM USE command.
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/download/nightly nvm install 8
In a later post we will setup a MySQL to node database connector.