Install a Node project on macOS

Every journey starts with a step…

Steffo Dimfelt
3 min readOct 9, 2020

It is dead easy to install Node.js and start up your first project.

The Journey

  1. Preparation
  2. Download Node.js
  3. Install Node.js
  4. Set up you first project
  5. Run Node.js

Hey! That’s easy!

1. Preparation

To make it easy for you later on. You need to install:

  • Visual Studio Code (or some kind of editor)
  • Yarn (If you want it, instead of npm. npm will be installed along with Node.js)

2. Download Node.js

Just go to nodejs.org and make your choice.
The LTS is recommended, because it contains the latest stable version.

Big buttons makes it super-easy!

3. Install Node.js

You now have a node package. Double click on the .pkg file and start the installation. Just click on every “Agree”-, “Install”- and “Continue”-button you’ll see.

4. Set up you first project

Open up you terminal and make a new folder.

mkdir myproject

Move into the folder

cd myproject

Install a Node module in the folder with npm or yarn.

You can do it the long way, and answer some questions:

npm init
or
yarn init

Or the short way, with some pre-defined answers (that you can change later on if you want to):

npm init --yes
or
yarn init --yes

5. Run Node.js

Okay. Time to open up the folder in the editor. There are only one file — the package.json and that file will manage everything that is relevant for the whole project. But it can’t show anything.

Lets create a new file called index.js. In that file we will do a simple “Hello World!” — why not? Every journey…

To make this fabulous project, just print:

const http = require(‘http’);http.createServer((request, respond) => {
respond.writeHead(200, {‘Content-Type’: ‘text/plain’});
respond.end(‘Hello World!’);
}).listen(3000);

So, what do “all” this codes means? Let us break it down.

const http = require(‘http’);

This module lets Node transfer data to the web via HTTP.

http.createServer((request, respond) => {
...
}).listen(3000);

In the module http, there is a method called createServer() that sets up a server. At the end of createServer() we then hook on another method called listen(). Inside listen() there is a number, 3000. This number is the port that the server will listen to. The number can be any number of your choice.

There are two objects in createServer(). The request and the respond. The request is used as input to createServer() and respond is used as output. They are often shorten to req and res. In this “Hello World”-example we will only use the respond.

respond.writeHead(200, {‘Content-Type’: ‘text/plain’});
respond.end(‘Hello World!’);

Inside the createServer() there are two lines. The lines have the respond-object to them, indicating that they will be used as outputs.

respond.writeHead(200, {‘Content-Type’: ‘text/plain’});

The writeHead will format the output to a plain text. If we want a HTML, it would have ‘text/html’ as a Content-Type.

The number 200, is a status code and indicates that everything is okay.

respond.end(‘Hello World!’);

Indicates the final output on the screen. In this case we will print out a string. Easy-peasy.

Now, let’s rock this baby!

Open up the terminal (I assume that you’re still in your project folder) and write:

node index.js

Open up your browser at http://localhost:3000/

Voila!

Another Masterpiece!

--

--

Steffo Dimfelt

Twentyfive years of graphic design. Six years of development. A lifetime of curiosity.