0% found this document useful (0 votes)
18 views17 pages

Unit 2

Uploaded by

jayvaghela1542
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views17 pages

Unit 2

Uploaded by

jayvaghela1542
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

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');

Open Node.js command prompt and run the following code:

1. node console_example1.js

We can also use format specifier in console.log() function.

File: console_example2.js

1. console.log('Hello %s', 'JavaTpoint');

Open Node.js command prompt and run the following code:

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. console.error(new Error('Hell! This is a wrong method.'));

Open Node.js command prompt and run the following code:

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. const name = 'John';


2. console.warn(`Don't mess with me ${name}! Don't mess with me!`);

Open Node.js command prompt and run the following code:

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.

Eval: It takes and evaluates the data structure.

Print: It prints the result.

Loop: It loops the above command until user press ctrl-c twice.

How to start REPL


You can start REPL by simply running "node" on the command prompt. See this:

4
Shri V J Modha College Of IT
Unit 2
You can execute various mathematical operations on REPL Node.js command prompt:

Node.js Simple expressions


After starting REPL node command prompt put any mathematical expression:

1. Example: >10+20-5
2. 25

1. Example2: >10+12 + (5*4)/7

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:

Node.js Multiline expressions


Node REPL supports multiline expressions like JavaScript. See the following do-while loop
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

Node.js Underscore Variable


You can also use underscore _ to get the last result.

Example:

7
Shri V J Modha College Of IT
Unit 2
Node.js REPL Commands
Commands Description

ctrl + c It is used to terminate the current command.

ctrl + c twice It terminates the node repl.

ctrl + d It terminates the node repl.

up/down keys It is used to see command history and modify previous commands.

tab keys It specifies the list of current command.

.help It specifies the list of all commands.

.break It is used to exit from multi-line expressions.

.clear It is used to exit from multi-line expressions.

.save filename It saves current node repl session to a file.

.load filename It is used to load file content in current node repl session.

Node.js Exit REPL


Use ctrl + c command twice to come out of Node.js REPL.

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:

o It provides online repositories for node.js packages/modules which are searchable


on search.nodejs.org
o It also provides command line utility to install Node.js packages, do version
management and dependency management of Node.js packages.

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

Installing Modules using npm


Following is the syntax to install any Node.js module:

1. npm install <Module Name>

Let's install a famous Node.js web framework called express: 10s

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.

Open the Node.js command prompt and execute "npm ls":

12
Shri V J Modha College Of IT
Unit 2

Globally installed packages/dependencies are stored in system directory. Let's install


express module using global installation. Although it will also produce the same result but
modules will be installed globally.

13
Shri V J Modha College Of IT
Unit 2
Open Node.js command prompt and execute the following code:

1. npm install express -g

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:

1. npm uninstall express

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.

1. npm search express

16
Shri V J Modha College Of IT
Unit 2

17
Shri V J Modha College Of IT

You might also like