Getting Started with Node.js: Part 1
- First, go to https://nodejs.org/download. As of this publication, you should see a screen that looks something like this with your options:
- You need to select the right version and you're on your own here. The good people at nodejs.org have created installers which I suggest choosing and running. If you want to use one of the binaries, you probably don't need this tutorial.
- Run the installer and follow the trail.
- Congrats. You have now installed node on your Mac.
- To get to the node.js command shell, open up a terminal window. If you haven't done this before, type command+space and enter "terminal"
- Depending on your version of MacOS, click/double click on your terminal icon to open it. You will see a window like this:
- Node is installed locally so you can type “node” from any directory to run the node.js command shell. Nice work!
- Let's make a "Hello world" application.
- Open a text editor. If you have Sublime text, Coda, etc. you can use that. If not, open up TextEdit which ships with every Mac. You can just type command+space again and search for "textedit" to find it. Again, depending on your version of MacOS, click/double click to open.
- We want this to be a plain text document so if you have icons at the top of your screen, type command+shift+T to go to plain text.
- Enter this text without anything else:
- Save the file as hello.js to your desktop.
- Now go back to your terminal window (it's an ugly black square in your toolbar). Type in:
- This will get us to your desktop within the terminal. Now type:
to run it.
Woah!! You just said Hello World through node.js.
Note, if you are using Textedit to make this file, it is critical that you switch to plain text before making the file. Text Edit, like most Document editors, will turn your quotes/ticks into these things (squiggly): ‘. Your program will not function. Most developers use text editors with more features and functionality, which is why I strongly advise checking out Sublime Text 3 or TextWrangler. Both are free (with paid versions if you want the "Upgrade now!" window to stop popping up, but that's no big deal).
Stay tuned for more in the "Getting Started" series!