Unit 2
Unit 2
Node.js Console
The Node.js console module provides a simple debugging console similar to JavaScript
console mechanism provided by web browsers.
There are three console methods that are used to write any node.js stream:
1. console.log()
2. console.error()
3. console.warn()
Node.js console.log()
The console.log() function is used to display simple message on console.
File: console_example1.js
1. console.log('Hello JavaTpoint');
1. node console_example1.js
File: console_example2.js
1
Shri V J Modha College Of IT
Unit 2
1. node console_example2.js
Node.js console.error()
The console.error() function is used to render error message on console.
File: console_example3.js
1. node console_example3.js
2
Shri V J Modha College Of IT
Unit 2
Node.js console.warn()
The console.warn() function is used to display warning message on console.
File: console_example4.js
1. node console_example4.js
3
Shri V J Modha College Of IT
Unit 2
Node.js REPL
The term REPL stands for Read Eval Print and Loop. It specifies a computer environment
like a window console or a Unix/Linux shell where you can enter the commands and the
system responds with an output in an interactive mode.
REPL Environment
The Node.js or node come bundled with REPL environment. Each part of the REPL
environment has a specific work.
Read: It reads user's input; parse the input into JavaScript data-structure and stores in
memory.
Loop: It loops the above command until user press ctrl-c twice.
4
Shri V J Modha College Of IT
Unit 2
You can execute various mathematical operations on REPL Node.js command prompt:
1. Example: >10+20-5
2. 25
5
Shri V J Modha College Of IT
Unit 2
Using variable
Variables are used to store values and print later. If you don't use var keyword then value
is stored in the variable and printed whereas if var keyword is used then value is stored
but not printed. You can print variables using console.log().
Example:
1. var x = 0
2. undefined
3. > do {
4. ... x++;
5. ... console.log("x: " + x);
6. ... } while ( x < 10 );
6
Shri V J Modha College Of IT
Unit 2
Example:
7
Shri V J Modha College Of IT
Unit 2
Node.js REPL Commands
Commands Description
up/down keys It is used to see command history and modify previous commands.
.load filename It is used to load file content in current node repl session.
8
Shri V J Modha College Of IT
Unit 2
9
Shri V J Modha College Of IT
Unit 2
Node.js Package Manager
Node Package Manager provides two main functionalities:
The npm comes bundled with Node.js installables in versions after that v0.6.3. You can
check the version by opening Node.js command prompt and typing the following
command:
1. npm version
Open the Node.js command prompt and execute the following command:
10
Shri V J Modha College Of IT
Unit 2
1. npm install express
You can see the result after installing the "express" framework.
11
Shri V J Modha College Of IT
Unit 2
Global vs Local Installation
By default, npm installs dependency in local mode. Here local mode specifies the folder
where Node application is present. For example if you installed express module, it created
node_modules directory in the current directory where it installed express module.
You can use npm ls command to list down all the locally installed modules.
12
Shri V J Modha College Of IT
Unit 2
13
Shri V J Modha College Of IT
Unit 2
Open Node.js command prompt and execute the following code:
Here first line tells about the module version and its location where it is getting installed.
14
Shri V J Modha College Of IT
Unit 2
Uninstalling a Module
To uninstall a Node.js module, use the following command:
The Node.js module is uninstalled. You can verify by using the following command:
1. npm ls
15
Shri V J Modha College Of IT
Unit 2
You can see that the module is empty now.
Searching a Module
"npm search express" command is used to search express or module.
16
Shri V J Modha College Of IT
Unit 2
17
Shri V J Modha College Of IT