Command Line (CL) Options in node.js

The tutorials provides the available node.js command line (CL) options to execute commands. The node.js CL provides help options and node.js version commands also.

Below given is the total list of node.js command -line (CL) options

S. Nonode.js Command -line (CL) Optionsnode.js command-line description
1v, –versionprints the current node.js version number
2-h ,–helpprints command line help options
3-e, –eval “script”evaluates the argument in JavaScript, predefined modules can also be used in script
4 -i, –interactive enables node interactive mode
5 -r, –require module preloads the required module at the startup which could be a path to the file or the node module name.
6 -p, –print “script” print /display the result
7 -c, –check script syntax check without executing
8 –no-warnings silence process warnings including deprecation
9 –trace-warnings displays stack trace for the warnings
10 –no-deprecation silence deprecation warnings
11 –trace-deprecation displays stack trace for the deprecation
12 –throw-deprecation throws deprecation errors
13 –trace-sync-io prints a stack trace whenever synchronous i/o is detected after the first turn of the event loop
14 –zero-fill-buffers Automatically zero-fills all newly allocated buffer and slowbuffer instances
15 –track-heap-objects tracks heap object allocations for heap snapshots
16 –prof-process processes V8 profiler output generated using the v8 option
17 -V8-options display V8 command line options
18 –enable-fips enables fips-compliant crypto at startup . Requires node.js to be built with ./configure –openssl-fips
19 –force-fips forces fips-compliant crypto on startup
20 –icu-data-dir=file specifies ICU data load path. (Overrides node_icu_data)
21 –tls-cipher-list=list specifies an alternative default tls cipher list. Requires node.js to be built with crypto support. (default)

Learn REPL Tutorial in Nodejs

The tutorial provides insight on the node.js REPL( Read Eval Print Loop) Tutorial. The node.js REPL provides the console to execute the commands similar to Windows Console or Linux Console where executed commands receives the response / output in an interactive mode.

Let’s discuss REPL( Read, Eval, Print and Loop) in details. REPL performs the below given tasks:

REPL command REPL command Description
ReadReads user entered input data, parses the input into JavaScript data structure, and stores in memory
Evalevaluates the java Script data structure
PrintPrints / Display the response/ output
Loopexecutes the print command till user presses Ctrl+C twice

How to start REPL

To start the REPL, you need to enter the “node” command in the node.js command prompt

Node.js – create expressions using REPL

Node.js command prompt is to be used for entering the expressions by executing the below given example

Node.js – variable assigment using REPL

The Node.js allows to create variables to store the value like other scripting languages. The var command is used to create variable but values store using var cannot be printed directly and we need to use the console.log() for printing variable values.

The variable beta value is printed but using the console.log function.

If var keyword is not used , then the value is stored in the variable and printed. The variable aplha is only printed with the value.

Nodejs – REPL underscore variable

The underscore (_) variable is used in node.js to get the last result

The variable sum uses (_) which captures the sum value of alpha and beta

Node.js multiple line expressions ( Loop in REPL)

Node.js provides the multiple line expression just like other scripting language . The common example could be a do..while loop where the value of alpha is incremented .

Node.js REPL commands

The below given are the avaialbel node.js REPL commands

REPL commands REPL command Description
ctrl +cterminates the current command
ctrl+ c ( 2 times)terminates the node REPL
ctrl +d terminates the node REPL
up /down keysup key -> displays command history
down key -> modify the previous command
tab key displays list of current commands
.helpdisplays list of help commands
.break exit from multiple line expression
.clearexit from multiple line expression
.save <filename>saves the node REPL Session into the file
.load <filename>loads the saved file content in the current node REPL Session

Components and Modules in Node.js

We have seen the Nodejs Tutorial steps in the previous blog. Now lets understand the main and commonly used components and the modules in the Node.js and its usage in the Node.js application.

Node.js Components

Node.js uses the below given components:

  • Import required modules: Node.js application use the require directive to load the node.js modules

var http = require(“http”);  

  • Create Server: The server which listens to the node.js application request
  • Request & Response: The node.js server will intercept the requests from the browser / console and returns the response back to the client application.

Refer the below program which makes the use of these components

var http = require(‘http’);
var requestListener = function (req, res) {
res.writeHead(200);
res.end(‘Hello, World!’);
}

var server = http.createServer(requestListener);
server.listen(8080, function() { console.log(“Listening on port 8080”)})

Node.js modules

Node.js is built on JavaScript framework and creates .js files for processing. Node.js modules provides the functionality to add the encapsulation logic for segregating the functionality into different modules. Node.js provides the below available modules :

  • Express Framework: The provides the features to develop robust web and mobile applications
  • MongoDB: provides support for the MondoDB and Node.js integration for database operations
  • Restify: Framework to support REST APIs integration
  • Jade: provides high performance template engine for nodes and browsers
  • Bluebird: provides promise library with full of feature and performance enhancements
  • Socket.io: provides real -time bi-directional event based communication, mpostly use for developing chat application

Node.js build modules

Node.js provides the in-built modules for processing the java script application.

Module Name Module Description
assertProvides a set of assertion tests
bufferhandles binary data
child_processfor running the child processes
clustermanages multiple node processing
cryptomanages OpenSSL cryptographic functions
dgrammanages UDP datagram sockets
dnsmanages DNS Lookup and name resolution functions
domaindeprecated now.
eventsmanages events
fsmanages file system
httpmanages node.js to run as HTTP Server
https manages node.js to run as HTTPS Server
nethelps in creating server & clients
osprovides operating system data
pathmanages file path
punycodedeprecated now
querystringmanages URL query strings
readlinemanages readable streams one line at the time
streammanages streaming data
string_decoderdecodes buffer objects to strings
timersexecute a function after a given number of milliseconds
tlsmanages TLS and SSL protocols
ttyProvides classes used by a text terminal
urlparse URL strings
utilprovides utility functions
v8accesses information about V8 (the JavaScript engine)
vmcompiles JavaScript code in a virtual machine
zlibcompress or decompress files

console module in node.js for debugging

The tutorial provides the debugging mechanism provided by node.js console module and the console methods which can be used based on programming requirements.

node.js console methods

The node.js console module provides the below console methods used in the node.js stream

  • console.log ( ) – used for displaying the message / output on the console.

Example – Enter the below command and save the file as “hello.js”

console.log(‘ Hello oracleappshelp users. Happy Learning’);

Execute the file -> node hello.js

  • console.error ( ) – used for displaying the error message on the failure of the processing.

Example: below command depicts console.error message.

console.error(new Error (‘ERR1001 – The data processing is failed’));

  • console.warn ( ) – used to display the Warning / Informational message.

Example: below command depicts console.warn message.


Node.js installation in Windows /Linux OS

To proceed further in details about nodejs , lets go through the step by step Nodejs installation. NodeJs is cross-platform can thus can be installed from Windows to Ubuntu and OS X. Nodejs framework provides support for embedding external functionality though available modules or through custom modules like Nodejs provides integration with MongoDB and with MySQL for the database operations.

Nodejs windows installation

Nodejs installation process includes the installation of Nodejs libraries on the client system to make the environment ready for development.

  1. Download the stable Nodejs version from the official site https://nodejs.org/en/download/ . Latest LTS Version: 12.16.3 (includes npm 6.14.4)
  2. Check if the Windows system is 32-bit or 64-bit processor

3. Select installer and get it downloaded.

4. Run the executable and it will open the nodejs setup screen.

5. Click Next

6. Accept the Terms for License Agreement

7. Specify the location for the Nodejs installation folder in Windows Directory. Click Next

8. Accept the default component ( Nodejs runtime ) and Click Next

9. Click Next

10. Click Install button

11. Nodejs installation setup completed .

12. Click Finish button

Installation on UNIX/Linux/Mac OS X, and SunOS

  1. Download the stable Nodejs version from the official site https://nodejs.org/en/download/ . Latest LTS Version: 12.16.3 (includes npm 6.14.4)
  2. Linux and Mac OS respective installer file

Mac OS : node-v12.16.3-darwin-x64.tar.gz
Linux OS : node-v12.16.3-linux-x64.tar.gz

3. Execute the below commands for the installation

$ cd /tmp
$ wget http://nodejs.org/dist/v12.16.3/node-v12.16.3-linux-x64.tar.gz
$ tar xvfz node-v12.16.3-linux-x64.tar.gz
$ mkdir -p /usr/local/nodejs
$ mv node-v12.16.3-linux-x64/* /usr/local/nodejs

4. Add /usr/local/nodejs/bin to the PATH environment variable

export PATH=$PATH:/usr/local/nodejs/bin


Top Node.js tutorial for beginners

Node.js is one of the most popular JavaScript Framework, is a server-side platform developed by Ryan Dahl in 2009 and built on Google Chrome’s JavaScript Engine (V8 Engine). Node.js provides the cross-platform runtime environment , driven on events and executes asynchronous transactions helps in building I/O intensive web applications, single-page applications, server based and network applications with ease. Node.js is open source, completely free, and used by thousands of developers around the world.

Node.js tutorial covers topics on Node.js installation on windows and linux, REPL module in Node.js, NPM module in Node.js, Node.js callbacks examples , Node.js event loop examples, Node.js OS module examples , Node.js Path module examples , Node.js query string examples, Node.js URL examples , cryptography in Node.js, Node.js debugger examples , DNS module in Node.js , Node.js Net module example, Node.js UDP example, process example in Node.js , child processes in Node.js, buffer module example in Node.js, streams example in Node.js, file system module in Node.js, global objects in Node.js, NodeJS web modules , module level object scope in Node.js

Node.js tutorial provides the basic and advanced concepts of Node.js.Knowledge of HTML /CSS/ AJAX should be useful.

Lets consider a simple server based processing scenario and where node.js executes client request in asynchronous fashion and eliminates the waiting time to be ready for processing next request.

PHP / ASP/ Java
based server processing
Node.JS server processing
Client submit the data request for processingClient submit the data request for processing
Client waits till the request is getting processedNo wait is required at client side
Server processes the request and sends it back to the Client Client is ready to process next request
Client is ready to process next requestWhen Server processes the request and sends it back to the Client

which conlcudes that Node.js runs single-threaded, non-blocking asynchronously programming and provides better memory efficiency.

What node.js provides to the developers ?

The below given are the features which makes it more popular among developers

  • Node.js helps in developing dynamic page content
  • Node.js provides numerous file based operations (create, open, read, write, delete, and close files on the server)
  • Node.js helps in collecting form data
  • Node.js provides feature for CRUD operations (Create, Read, Update ,Delete on the database

Node.js Download

The node.js can be downloaded from the Node.js official web site https://nodejs.org

Node.js sample file run

Once the successful installation, try to validate the node.js by executing the below sample program.

var http = require('http');
var requestListener = function (req, res) {
  res.writeHead(200);
  res.end('Hello, World!');
}
var server = http.createServer(requestListener);
server.listen(3000, function() { console.log("Listening on port 3000")});

Node.js Tutorial Topics

The below given are the common topics for beginners to learn Node.js.

Node.js installation on Windows / Linux OS

Tutorial explains the step by step process for installing node.js on windows / Linux OS

Components and module in Node.js

Tutorial explains the usage of components and module in node.js with examples

Punnycode module in Node.js

Tutorial explains the node.js punnycode module with examples

String Decoder in Node.js

Tutorial explains the node.js string decoder with examples

Query String in Node.js

Tutorial explains the node.js query string with examples

Console in Node.js

Tutorial explains the usage of console in node.js with examples

Path module in Node.js

Tutorial explains the path module in node.js with examples

TTY module in Node.js

Tutorial explains the tty module in node.js with examples

File System module in Node.js

Tutorial explains the file system module in node.js with examples

Streams module in Node.js

Tutorial explains the streams module in node.js with examples

TLS SSL module in Node.js

Tutorial explains the tls ssl module in node.js with examples

Crypto module in Node.js

Tutorial explains the crypto module in node.js with examples

Net module in Node.js

Tutorial explains the net module in node.js with examples

OS module in Node.js

Tutorial explains the os module in node.js with examples

DNS module in Node.js

Tutorial explains the dns module in node.js with examples

Error Handling in Node.js

Tutorial explains the error handling in node.js with examples

Buffer Class in Node.js

Tutorial explains the buffer class in node.js with examples

Global and module level objects in Node.js

Tutorial explains the scope for global objects and module level objects in node.js with examples

Command Line Options in Node.js

Tutorial explains the command line options in node.js with examples

REPL module in Node.js

Tutorial explains the REPL module in node.js with examples

Node.js MySQL Integration examples

Node.js example to create MySQL table

Tutorial provides the node.js integration program to create MySQL table

Node.js example to drop MySQL table

Tutorial provides the node.js integration program to drop MySQL table

Node.js example to Insert records into MySQL table

Tutorial provides the node.js integration program to insert record into MySQL table

Node.js example to Select records from MySQL table

Tutorial provides the node.js integration program to select record from MySQL table

Node.js example to Update records into MySQL table

Tutorial provides the node.js integration program to update record into MySQL table

Node.js example to Delete records from MySQL table

Tutorial provides the node.js integration program to delete record from MySQL table