Making your first program in Node.js macOS

Marcelo Lopes
2 min readMar 25, 2021

The first thing to do is download Node.js on https://nodejs.org/en/download/, choose the recommended version.

To write and compile the program we used Sublime Text, you can use other text editor of your choice.

Our first program will be que famous “Hello, World!”.

Open Sublime Text and write the following code:

console.log(“Hello, World!”)

Save the file as “Hello.js”, it should look like this:

Next, you have to open you terminal, and type:

cd path_of_the_file

The “path_of_the_file” is the directory where you saved your “Hello.js” file.

After that you type:

node Hello.js

This command will run the statement on the “Hello.js” file and show the output on the Terminal like the image below:

That’s how we create our first program in Node.js.

Another option you have to make the “Hello, World!” is to use a HTTP server.

Open Sublime Text and write the following code:

var http = require(“http”);
http.createServer(function (request, response){

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

response.end(‘Hello, World!\n’);
}).listen(8081);

console.log(‘Server running at http://127.0.0.1:8081/');

Save the file as “server.js”, it should look like this:

Next, you have to open you terminal, and type:

cd path_of_the_file

The “path_of_the_file” is the directory where you saved your “server.js” file.

After that you type:

node server.js

Finally you open your browser and you type the following link http://localhost:8081/ and you get the result shown in the image below:

--

--