Let’s start using Node.js, seeing how to create our first script in this environment and run it from the command console.
Don’t worry if it’s your first time with Node.js. One of the reasons for its fame is how well it works and how simple it is. So, fear not.
Creating the Script
Now simply create a folder anywhere on your computer. Inside it, create a new file called, for example, hola_mundo.js. The name doesn’t matter, but the extension must be js (JavaScript).
Now, let’s open this file with any text editor, or with our favorite IDE, and inside it, we put this, hola_mundo.js, we write the following code:
console.log("Hello World in Node.js!");
In this code, we use the console.log() function from Node.js to print “¡Hola Mundo en Node.js!” to the console. It’s not a big deal, but as a first program, it’s not bad!
Running the Program
Once we have written our script in our file, we can call Node.js to execute it using the following command from a terminal console.
node hola_mundo.js
This command tells Node.js to run the hola_mundo.js script. You will see the message “¡Hola Mundo en Node.js!” printed to the console as a result.
File Extensions: .js vs .mjs
We said files must have the .js extension. Actually, they can be .js or .mjs.
.jsis used for traditional script files.mjsis used to indicate the file is a JavaScript module
For now, we will focus on .js files, but we will explore JavaScript modules in a later article
Management with NPM
Node.js also comes with NPM (Node Package Manager), which is a package manager for installing and managing Node.js dependencies.
NPM also helps us manage and initialize projects. Its use is so important in development today that you have a whole course for NPM at this link 👇
Learn how to use the NPM package manager
Download the Code
All the code for this post is available for download on Github
