Install Node.js

Follow the official website guide to install Node.js.

Get Version

node -v

If you ever wanted to know what version of Node you are using, run node -v and it will return the Node version installed in your system. There is another way to see version. The version property of the global object process can be used to get the version -

console.log(process.version)

How to run Node.js Code

Node is an interpreted language and environment, not a compiled one. There are three main way to launch Node code:

REPL

By simply typing node on your console, you will get into REPL Environment. In which you can type node statement and press Enter to exectue that statement. It is called REPL Environment because the statement is evaluated as you type Enter and the output is generated instantly. To get out of this mode press CTRL+C.

node

Eval

node -e statement

You can use -e to execute Node statement. The argument statement is the statement to execute. Note that it must be string. This is useful when you want to check a very small chunk of code and get the output immediately. Here -e stands for eval. So this command actually executes an inline javascript statement right after the node command.

node -e "console.log('Santanu Bera')"
> Santanu Bera
node -e "console.log(new Date())"
node -e "console.log(process.versions.node)"

Node eval (-e flag) is useful in npm and bash scripts because it allows you to execute Node in a very compact manner in the bash, zsh or any other shell environment without having to have a Node file. You can get versions, OS information or run any Node code such as working with a file system, database or HTTP.

Launching Node Code from a File

This is the most common use case because it allows you to save long Node programs and run them. To run a Node.js script from a file, simply type node filename.js.

node filename.js

or simply,

node filename

For example, to launch code from a program.js file which is located in a current folder, simply execute:

node program.js

The file must be in the same folder. If you need to execute code from a file which is in a different folder, just provide the relative or absolute path:

// Relative Path
node ./app/server.js


// Absolute Path
node /var/www/app/server.js

Node.js Globals

Despite being modeled after one standard, Node.js and browser JavaScript differ when it comes to globals. As you might know, in browser JavaScript we have a window object. However, in Node.js, it is absent (obviously we don’t deal with a browser window), but developers are provided with new objects/keywords:

There is a variable named global which is accessible by any Node script or program. It refers to the global object. This object has properties. For example global.process or global.require or global.console.

Any first level property of the global object is accessible without the global prefix. For example, global.process and just process will be the same.

global.process

// same as

process

There are few properties of global object which is used very often. Some of them are -

As we progress through this tutorial, we will know more about them.

GLOBAL

The GLOBAL alias for global can be seen in older project but is deprecated. Use global instead of GLOBAL.