Setting up a Node.js Environment

Sijuade Ajagunna
devcareers
Published in
4 min readSep 12, 2019
https://medium.com/@nileshsingh

So you’ve decided to set up Node.js on your computer for a number of reasons — maybe you’re interested in learning server-side web development or you need the package manager (npm) that comes with it for other projects. This article aims to help simplify the process of downloading, installing and running node.

What is Node?

According to the official website, Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. In simpler terms, Node helps you use JavaScript in backend web development to build servers that can interact with file systems and connect to databases. It runs on the same powerful engine that powers Chrome — the V8 engine.

So using Node, will the same JavaScript codes that run on browsers run on the server side?

Not really. If you’re a bit familiar with the front end, you might already know that JavaScript interacts with the browser using an API. This API allows browser specific commands like querySelector() and appendChild() run on the browsers. These APIs won’t work on NodeJS but it incorporates APIs that can communicate with HTTP, file systems etc.

Enough of all the talk, It’s time to set up Node.js!

First, to download Node.js, navigate to the Node website here. The homepage should look something like this:

Edit: Dec 26th 2019, Please note that version 12.14 is now the LTS version for nodeJS and should be downloaded instead of 10.16. Thanks.

To download or not to download?

Now you have 2 choices, To go with the LTS(Long Term Support) version (on the left) or with the current version? The Current version has newer experimental features (it supports more ES6 features than the LTS) while as the name suggests, updates and fixes will be provided for the LTS version in the long run compared to the LTS version.

For this article, we’ll be working with the 10.16.3 LTS version as there are packages that can help us write the latest ES6 codes which we will discuss in a later article.

Installation.

Once you click on your desired version, the .exe file should start downloading automatically. Run the file to install and keep clicking next till it installs completely.

To check if the installation is successful, open your Command Prompt or Terminal if you’re on a Mac, then type ‘node -v’, It should display your node version, else, If you see something like ‘node: command not found’ then you may need to re-install it.

It worked? Good! What Next?

A Text Editor — if you don’t have one already. There is an abundance of available software for writing and editing code like Visual Studio Code, Brackets, Sublime Text etc. My preferred editor is VS Code and you can get it at the official website.

Writing your first Node.js Script

Create a folder in your desired directory and open it in your code editor. Once opened, inside your folder, create a file and save it as ‘file-name.js’.

.js? Yeah .js! As I mentioned earlier, Node.js is NOT a programming language. It is just JavaScript that runs on the server side! So you do need to have a bit of JS knowledge to work with Node.js. You can start learning at freecodecamp today if you want to brush up on your knowledge of JavaScript.

So back to writing our first script. Let’s start with a simple JS command.
console.log(‘Hey World’);

So how do you run this?

Ordinarily, when you write JS on the front end, It runs and prints to the browser console, in Node.JS however, your console is your terminal.

Interacting with the terminal
If you use VS Code like I do, you should find ‘Terminal’ in the top menu. Click on it and on the dropdown menu, select ‘New Terminal’. Otherwise, you can open Command Prompt on windows or ‘Terminal’ for Mac.

Once your terminal is open, type ‘pwd’ to print your current directory. In VS Code terminal, it should be your your current folder. If you have to open command prompt or terminal however, you will get another directory. if this happens, you’ll need to use ‘cd’ to migrate to your folder location. You can get this location by navigating to the folder and then copying the location from the navigation bar of the windows menu.

Go back to your Terminal/Command Prompt, type cd and then paste your folder location e.g.:
cd C:\Users\USER\Documents\node-folder (node-folder is my folder name)

PS: IT SHOULD BE YOUR FOLDER NAME AND NOT YOUR FILE NAME.
‘cd’ and ‘pwd’ are Command Prompt/Terminal commands you can learn more about Windows commands here or if you use a mac, here.

Once this opens, you’re where you should be and we can now run your file. To run a file, simple type ‘node’ followed by the name of your file.

node file-name.js
//Hey World

And so, Node.js is successfully set up and running on our computer. In our next article we’ll learn about npm and how to use npm packages to run the latest ES6 features on Node.js.

You should have something like this

--

--