Npm Hello World

Posted on  by admin

Do you use Node…? You will.

This article is aimed for beginner developers and anyone interested in getting up and running with Node.js.

Before diving into this article, you should be confident enough with JavaScript to know the basic concepts of the language.

Technical terms regarding Node will be explained and linked below. Node is an asynchronous event driven JavaScript runtime built upon Chrome’s V8 JavaScript engine.

It’s designed to build scalable network applications. That being the raw definition, let me clarify.

Node.js enables you to write server side JavaScript. You may now be wondering, how? As you know, JavaScript is a language which runs in a browser.

The browser’s engine takes JavaScript code and compiles it into commands.


The creator of Node.js took Chrome’s engine and built a runtime for it to work on a server.

Don’t get confused with the word runtime. It’s an environment where the language can get interpreted.

Web-based Hello World Example

So what do we have now? A way to write JavaScript on the back end. Regarding the definition, you might be wondering what the term asynchronous even means in the current context.

JavaScript is single threaded, meaning there is only one thread of execution.

Console-based Hello World Example

So you don’t want events to interrupt the main thread of execution.

This is what asynchronous means, handling events without interrupting the main thread.

Node is based on this non-blocking execution, making it one of the fastest tools for building web applications today.

In the following “Hello World” example, many connections can be handled concurrently.

Upon each connection the callback is fired, but if there is no work to be done Node will remain asleep.

There are 6 simple steps in this example, bear with me. Install Node.js for your platform (MacOS, Windows or Linux).

Step 4:

The first step is to get yourself an instance of the JavaScript runtime up and running on your local machine.

Just smash in your browsers address bar, or click the link above, and you should be good to go.

The home screen should give you what you want right away. As I am running Ubuntu on my machine, the respective version of Node.js for my operating system is listed.

Go ahead, download and install it.

This will give you the tools needed to run a server on your local machine.

These commands are universal for whatever OS you’ll be running. The former will create a new directory inside the directory you are currently in, mkdir = “make directory”.

The latter will change into this newly created directory, cd = “change directory”. Hard-core windows users can calm down, this will work for you guys too, as it is equivalent to creating a new folder within your file system… only more fancy.

Initialize your project and link it to npm. Now the real fun starts. After creating your directory, very innovatively named myapp, you will need to initialize a project and link it to npm.

Okay, calm down. Npm is short for node package manager. This is where all node packages live. Packages can be viewed as bundles of code, like modules, which carry out a specific function. This functionality is what we as developers are utilizing. We use the application program interface, the API, provided for us by these modules.

What is an API you ask? The modules in turn act like black boxes with buttons and levers we can push and pull to get the desired end result.

Running this command initializes your project:. This creates a package.json file in your myapp folder.

The file contains references for all npm packages you have downloaded to your project.

Additional tips:

The command will prompt you to enter a number of things. You can enter your way through all of them EXCEPT this one:. You will want to change this to:.

Install Express in the myapp directory. While still in the myapp directory run:.

The install command will go ahead and find the package you wish to install, and install it to your project. You will now be able to see a node_modules folder get created in the root of your project.

This is a crucial step, as you will be able to require any of the recently installed files in your own application files.

The addition of —save will save the package to your dependencies list, located in the package.json, in your myapp directory.

Yeah, I know what you’re thinking. What exactly is this Express thing?