As the JS code was initially available to run only on browsers or JS engines. To run JS on a machine directly, a runtime environment is required as JRE for Java. NodeJS is a runtime environment to run JavaScript code outside the browser. Due to its association with JavaScript, NodeJS is quite popular among computing enthusiasts.
The primary purpose of NodeJS is to create web servers and the way of handling requests of NodeJS web servers outperforms the PHP or ASP-based web servers.
Initially, NodeJS support was available on Linux but later, NodeJS was made available for Windows and macOS users as well. This article will guide you to write and run the NodeJS program on mac. Additionally, you would also get to know about the installation of NodeJS on mac.
How to install NodeJS on Mac
To write a NodeJS program on mac, it must be installed on your machine. This section will guide you to install NodeJS on your Mac.
Step 1: Navigate to the NodeJS website to get the required version of NodeJS. Variety of old releases and new versions are available. You can download the version as per your requirement.
Step 2: After downloading the required file, you would have to follow the similar steps as shown below to install NodeJS.
Accept the licence agreement.
It will ask for confirmation, click on Agree to continue.
Select the destination folder and click on Continue
Click on Install to go with the recommended settings
The installation process will start up
After successful installation, you will get the following interface. Click on Close
Step 3: After completing the installation, you must verify the version by opening the terminal (command+space) and writing “node -v” to check the version.
How to write and run a NodeJS program on Mac
Here, you will find a step-by-step procedure to write a NodeJS program on Mac. Let’s go through these steps.
Step 1: Open the code editor, create a new JavaScript(.js) file. In our case it is first.js.
Step 2: Write some code inside it as we did by using the following lines of code.
console.log(x+y);
In the above code, two string variables are created and printed using the console.log statement.
Step 3: Open the terminal, and write the following command to run the first.js file.
It is observed from the output that the result of the first.js file has been printed on the terminal. That’s the major contribution of NodeJS, as you do not need any browser to run the JavaScript files.
Conclusion
To write and run a NodeJS program on mac, first download and install the LTS version of NodeJS from its official website. After installation, create a new JavaScript(.js) file, write some code inside it, and run the “node fileName.js” command in the terminal. This post aims to write and run the first NodeJS program on Mac.