The first thing we need to do to start using building Node apps is to dowload and install the latest version of Node.js.
If you already have Node installed you can skip down to "Using Node in the Command Line". If you aren’t sure if you have Node installed, open a command prompt and run
node -v. If you get an error, you need to install Node.js.
If you don’t have Node.js installed on your computer, go to nodejs.org to download the installer. You should notice that the page has intelligently detected your operating system and will offer the right download links for you. I’m using a Mac, so here is what I see:
There are two green download buttons on the page. One is labeled “LTS” for “Long Term Support”. The other is labeled “Current” which means it’s the latest release but it might not be suitable for production applications that need long term stability. Go ahead and download the the “Current” (latest) version since we will be using it for learning purposes. When you click the download button, it should download an installer program. Open it up and follow all of the steps. It should be pretty straightforward.
Once you’ve installed Node, you should be able to use it in your favorite command line tool. Let’s make sure the
node command is available to us. Open up a command prompt window and type
node -v. Then, hit the ‘Enter’ key to execute the command. This command checks what version of Node you have installed and is a great way to make sure you have it installed properly. If all goes well you should see a response in the command line that says
v9.2.0 or whatever version number you downloaded from the Node.js website.
If you get a response like
node: command not found then something went wrong during the installation so you’ll need to Google to find some troubleshooting tips or just ask me for help. :D
Using Node in the Command Line
Now that we’ve confirmed Node is installed correctly, we can use it right from the command line. Simply run
node in your the command line tool. If all goes well, your cursor should drop down to a new line, preceded by a right-facing caret (AKA greater than symbol, “>”).
1 + 1 and hit ‘Enter’. You should get the response
2. Try executing
document.getElementById();. There is also no such thing as
prompt(); in Node because these are browser based functionality, and Node doesn’t run in the browser.
Node also makes some new server-based functionality available to us, like
assert, and the file system (
fs). Don’t worry too much about what these are for right now. You’ll get into them later on in your Node journey. (If you are interested in learning more about all of the functionality available in Node, check out the full Node.js API documentation.)
Alright! You are off to a great start! Check out my next post about how to set up a Node.js project with NPM!