Getting Started with Node.js
You need to install the Node.js first to create and execute any application. You can download and follow the instructions from this link to install Node.js.
Running "Hello World" Node.js script on console
We can type short code in terminal to see some results,
console.log('hello world')
Hit Enter key
The output will be,
> console.log('hello world')
hello world
undefined
We can run small snippets of code with this interface without putting them into a file.
console.log('hello world')
First of all, create a file with index.js. For this we can open up any of the IDE (like Atom or Sublime), create a new file and save it with the name index.js.
Now just copy and paste,
console.log('hello world')
To run this file with Node.js, we need to open up the Node terminal again and navigate to the directory in which you placed index.js.
Navigate to the directory where index.js is stored by the cd command.
Type
node index.js
Hit Enter key
The output will be,
> console.log('hello world')
hello world
undefined
We can run small snippets of code with this interface without putting them into a file.
console.log('hello world')
Running Script as Node.js Application
First of all, create a file with index.js. For this we can open up any of the IDE (like Atom or Sublime), create a new file and save it with the name index.js.
Now just copy and paste,
console.log('hello world')
To run this file with Node.js, we need to open up the Node terminal again and navigate to the directory in which you placed index.js.
Navigate to the directory where index.js is stored by the cd command.
Type
node index.js
command and Hit Enter key to execute this code. Something like this will be appearing on your screen,