Let us see things one by one.
It is a state of target machine which enables you to run some program by providing certain libraries and modules to interact with the OS.
Q. But why did you tell me this? ¯\_(ツ)_/¯
For windows users
Visit the url and download the latest installation for your system. Download and install Windows Installer (.msi) [32bit or 64bit depending on your system] from the above link and you will have the latest version of NodeJS with NPM running on your system.
Visit the url & Download and install macOS Installer (.pkg) from the above link and you will have the latest version of NodeJS with NPM running on your system.
For Linux Operating System (Ubuntu)
Open your terminal and execute the following command:
$ sudo apt-get install -y build-essential
$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
$ sudo apt-get install -y nodejs
This would install NodeJS and NPM on your local system.
You can access the NodeJS terminal by executing:
> a = 25
> b = 35
Awesome. Now NodeJS is setup perfectly on your system.
Setting environment variables
This tutorial will focus on building a web server with NodeJS. So for this one more step is recommended. You need to add an environment variable NODE_ENV=”development” in your system so that the server knows that this system is a development environment and not for production.
Linux & MacOS:
1) Open .bash_profile file
$ nano ~/.bash_profile
Append the following
Note: There should be no space around the space character
3) Save the file and restart the terminal
1. Open Control Panel
2. Click System
3. Click Advanced System Settings
4. Go to Advanced tab
5. Click Environment variables… button
6. Add the variable NODE_ENV with value development
Setting up editor
- WebStorm: https://www.jetbrains.com/webstorm/
- Sublime Text: https://www.sublimetext.com/
- Atom: https://atom.io/
- And many more
I would recommend using WebStorm. It is a licenced software but can be used by students for free for educational purposes. Sublime and Atom also work as a charm for writing codes.
Download the installation files for your favorite text editor and install them. All of these have one click installation files. No need for any terminal commands.
Everything is set now. Let us start building.
Writing your first NodeJS Application
As is the convention, let's create an Hello world application.
In NodeJS console
Start the Node console by running
This will start the NodeJS terminal. Time to print “Hello World”. In the Node terminal, write
> console.log(“Hello World !!!”)
And there you go. You just wrote a program to print Hello World to the console. Press Ctrl+C twice or enter .exit to exit the terminal. Here is what the terminal would look like.
console.log(“Hello World !!!”) print the text “Hello World !!!” to the console as shown in line 3.
The return value of console.log() is undefined i.e. void . Hence the undefined in line 4.
process is a global object created by the NodeJS environment having various properties of the environment. (More on it later)
process.stdout refers to the standard output for the NodeJS process which here is the terminal.
process.stdout.write and console.log both print strings to the console. The main difference is that console.log appends a new line character at the end of the current line.
To run this script, open the terminal at the location of this file and start the script using
$ node index.js
You should see an output like: That is it. Your first application just ran successfully.
Congrats on running your first program on NodeJS.