This is a step-by-step process on installing Node.js on Mac OS X. There are several “getting started” tutorials online but I didn’t find much on Mac OS X. The ones I did find were a little confusing since I had no previous experience with Node.js. At the time of this tutorial, I’m running OS X 10.8.2 also known as Mountain Lion.
Installation
There are several different installation instructions using Homebrew and other methods but the easiest is to install the Mac Installer Package straight from Node.js. Once it is downloaded, open the package and follow the instructions. The default installation path is /usr/local/bin/node
but you can change that to the path of your choosing. The great thing about the Mac Installer Package is that it also installs Node Package Manager (npm) which, in the past, used to be a separate install. The default installation path for npm is /usr/local/bin/npm
.
Install Verification
To verify that Node.js was installed successfully, open up terminal and execute the following:
node -v
The output should look like v0.10.0
or whatever version of node is current at the time you install it. The same thing can be done to verify npm.
npm -v
Creating you first Node Project
Now you can create you first node project and confirm that node is working correctly. You can use the node sample project provided on the Node.js homepage. Create a new file called example.js
and add the following code:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
You can start the server by running the following command:
node example.js
Note: example.js must be saved under your user directory. If not, then you need to provide the entire path or change it using cd
.
To verify that its working, open a browser and go to http://localhost:1337/. You should see a “Hello World” message.
Kill the server
To kill the server enter Ctrl-C
(Ctrl + C) in terminal.
Thats all there is to it. You have successfully installed Node.js and ran you first node project. I have to admit after reading several online tutorials, I thought installing Node.js and creating my first project was going to be harder than this. The Installer Package really makes this an effortless process. Stay tuned for more Node.js tutorials.
20 Comments
I am also just getting started to NodeJS and my development machine runs Mountain Lion. Thank you.
I did not think it would be that easy either, thanks a bunch ๐
It’s also easy to install it with macports.
Thanks, your explanation was so clear and concise. Some day all people will speak this way…
Many thanks for this short and sweet tutorial. Got me up and running less than in 5mins ๐
[…] This is a step-by-step process on installing Node.js on Mac OS X. There are several "getting started" tutorials online but I didn't find much on Mac OS X. The ones I did find were a little confusin… […]
As a begineer, very easy to install and helpful tips to follow.Thank you vey much.
I wondered around only to find this article the most comprehensive one, thanks Heaps!!
I thought It’d be much harder. Thanks a lot. You helped me..
There seems to be missing instructions required to enable global installation of npm modules (as opposed to having them locally installed in ./node_modules
This is very clear and precise post for the beginner to start with. Thanks for posting.
Year 2016 and it still works. Thanks for the clear and correct instructions
Thank you, very clear..
Very clear instructions. Thank you.
Thanks for your instructions. It helped me a quite a bit. I also had followed this https://www.youtube.com/watch?v=BIVfpvPnU0U (from TechCBT). You might also want to include this for the benefit of future readers!
Thank you for the tutorial.
Great tutorial.
HI i have installed NPM but I have not installed NODEJS, When I try NODE -V on the terminal it is saying bad option -v
Thanks – I was looking for a way to run a simple program and your post provided it.
If you’re not signed in to OneDrive with any account, start OneDrive to connect a work or school account.