Install the tools
In order to run Node, it and the npm package manager must first be installed. A number of install methods exist including the pre-built installer available from nodejs.org and package managers like [Homebrew], but there are a number of reasons these methods are inferior.
To properly install and manage Node use the Node Version Manager (NVM). NVM is a small CLI bash script which manages multiple versions of node on one system and allows easy switching using simple commands.
First make sure you have a .bashrc
file in the users directory as NVM will need to update it.
In Terminal use curl
to download and run the bash installer file for NVM:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
NVM installs into the users directory /Users/username/.nvm
and adds the following to the users .bashrc file:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Test that NVM was successfully installed by checking it’s version number:
nvm --version
Use NVM to install node:
nvm install node
Test that Node was successfully installed by checking it’s version number:
node -v
Note
NVM manages all the versions of Node and npm. The files for all versions of these apps live under .nvm
in the users directory /Users/username/.nvm
.
The which
command can be used to see the path to the current app.
which node
which npm
Test node
With Node a test environment can literally be located anywhere. Choose a directory on your system and create a file named test.js
. Put the following content into it:
// Node test file.
let message = "Hello, World!";
console.log(message);
In Terminal change to the directory where the test.js
file exists and then execute it with Node.
node test.js
If Hello, World!
is returned, Node has been completely setup on the system.
The node
command with a file name as an argument will: load, read, compile and execute the instructions from the file. Since a line of JS code in the test.js
file prints the text “Hello, World!” to the console, the text is returned to the console/terminal.
Make a simple web server
Choose a directory on your system and create a file named test_app.js
. Put the following content into it:
// Test server app.
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello from NodeJS\n');
});
server.listen(port, hostname, () => {
console.log('Server running at http://' + hostname + ':' + port);
});
Execute the new test app by passing a file name to the node
command.
node test_app.js
If the text Server running at http://127.0.0.1:3000
is returned to the console, launch a web browser and go to the URL:
http://localhost:3000
If a white page with the text Hello from Node.js
appears, the simple server is working.
To stop the server simply Ctrl + C in Terminal. This will end the program gracefully and allow it to unbind from any ports it is listening on.