Setup Nodejs on Mac and build your first app

What is Nodejs?

Node is a C++ program or run-time environment that allows JavaScript code to run outside of a browser using Chrome’s V8 JavaScript engine. It is not a framework like Asp.net or Java and hence should be compared with them.

Node is asynchronous or non-blocking by nature. A single thread is used to handle all requests. It is ideal for I/O intensive apps. The thread doesn’t wait e.g. for the database query to complete before moving on to another request.

Node should not be used for CPU intensive apps like video encoding or image manipulation and the likes.


Chrome’s V8 embedded in Node

Setup Nodejs on mac?

  1. Go to the Download page for Nodejs.
  2. Download the LTS version .pkg Mac installer file.
  3. Open the .pkg file and follow the Installer steps and that’s it.
  4. Open terminal on Mac and run command “node -v” which displays the installed version on your machine. This will confirm whether the node runtime was properly installed.

You may need to update the npm CLI client as fixed version is installed with the LTS version, npm is the package manager for Javascript. Run the following command at the Terminal:

$ sudo npm install npm --global

Start exploring and build your first app. At the terminal, run the following commands:

$ cd Documents
$ mkdir node-course
$ cd node-course
$ mkdir first-app
$ cd first-app
$ touch app.js

The above commands brings us inside the new created folder first-app and created our JavaScript file app.js. Now open this file in your preferred editor like Sublime, atom, Visual Studio Code or Visual Studio for Mac. I’m using the Visual Studio for Mac Community edition. In the editor, add the below code to app.js:

function sayHello(name){
    console.log('Hello ' + name);
}

sayHello('Pulkit');

Back to Terminal, run the app:

$ node app.js

You should see the following output:

Hello Pulkit

Hence, you successfully run your first app using node on Mac.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.