Applications

Execute javascript code on windows thanks to nodejs

JavaScript is a language used primarily in web page development. This programming language is of the “frontend” type, that is, it runs directly on the user’s PC through the web browser. However, given its potential, it is gaining more and more importance outside of browsers. And this is made possible by the existence of very complete JS environments that allow us to run any program created in this code simply, as in the case of Node.js .

What is Node.js

JavaScript has been, for years, an exclusively frontend language. The browser reads and executes it as it is given on a web page. However, this programming language is much more complete, and it has nothing to envy to other programming languages ​​neither in functionality nor in performance. Of course, in order not to depend on the browser, it is necessary to have an interpreter. And this is where Node.js comes in.

Node.js is a JavaScript environment designed to run on servers. This environment is based on the Chromium V8 engine, and allows us to run, asynchronously, all kinds of JS code on the server without depending on a full browser.

When we run a JavaScript program in Node.js, what this environment does is load a virtual machine with Chrome’s V8 engine, and use it to interpret and execute the code. In addition, it has a great variety of modules so that we do not have to write all the functions from scratch, speeding up programming.

Some of the most common uses of this environment are:

  • Create APIs and backend systems that run on the server side.
  • Create frontend programs and tools that run in the console without depending on the browser.
  • Create desktop programs thanks to Node and a framework like « Electron «.

Install Node.js on Windows 10

This environment has two different versions. On the one hand we have the most current and most up-to-date version, which brings the latest features implemented in the environment, but it can be unstable. And on the other hand we have the LTS version , the most stable with extended support that will guarantee us the best possible performance and stability.

We can find both versions available on their website . We must choose the version we want, and download that best suits our operating system. For example, if we have a PC with a 64-bit Windows 10, we must download the 64-bit Windows MSI installer .

Once we have downloaded the installer we want, we run it on our computer. The installation process of this program is very simple, since all we have to do is follow the steps to the end.

At a certain point in the installation, it is important to make sure that the “add to PATH” option is correctly activated. In this way we can run the environment more easily from a CMD window.

In addition, we must also mark the option to download additional tools to be able to work much better with this tool. The additional tools that Node.js will download are Chocolatey, Python and Visual Studio Code. It will also update some elements of the system, such as .NET, so that the programs work correctly.

The installation process will now begin. We wait for it to finish and, when it finishes, we will have it ready.

If we have marked the option to download additional tools, a CMD window will automatically open, which will load a PowerShell script and all these tools will be downloaded and installed automatically, without having to do anything.

When finished, we will have Node.js installed on the computer and ready to use it.

Install it on Linux

This environment is also available for the main Linux distributions. On its website we can directly download the source code, or the binaries, that will allow us to make use of this tool. But, if we prefer, we can also install it from the terminal with a simple command:

sudo apt install nodejs npm

When it finishes, we will have the environment ready. We can check that it works, and check the version, by running “node -v” in a terminal.

Install it on macOS

If we are users of the Apple operating system, we will also be able to install this environment on macOS. To do this, what we must do is, on the download page, select the macOS version instead of the Windows version.

The macOS installer is a .PGK file, which we must execute with a double click to install it on our computer.

How to open Node.js

Once Node is installed, we can start working with it in different ways. If we check the option to add the tool to the Windows PATH, we can launch it both from CMD and from PowerShell by writing the command “node”, followed by the command, or script, that we want to execute.

Otherwise, we can launch the JavaScript console using the Windows 10 search engine, or from the corresponding entry in the start menu. Here we will find several sections. On the one hand, we will be able to open the Node console to be able to execute the instructions we want, or launch the scripts. On the other, open the NPM console to run scripts directly from it using this library. And, if we do not install the additional tools and dependencies at the time, from here we will be able to do it directly.

Once we know the parts of this installation, we are going to see how to execute our first backend with JavaScript thanks to Node.

Run a program

Once this environment is installed on our computer, the next step will be to learn how to execute the code. To do this, the first thing we will do is create our first program. If we already have a .js file on the computer, we can skip this step and test with it by running it directly from the Node.js console.

Otherwise, we will open a console window for this interpreter and write the following commands in it:

var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World');
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');

If all goes well, we will have our backend server running this interpreter. To check that it works, what we must do is open any web browser, such as Edge, and enter the following address:

http://127.0.0.1:8081/

We will see a message printed with the words “Hello World”, the most basic. From there, we can unleash our imagination and create the programs we want to run from the server.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button