Node.js Interview Questions Answers - Basic + Advanced
Tags: node, javascript, fullstackdeveloper
Published: 2020-06-07 11:27:20

<div class="intro-cls">Node.js Interview Questions: Node.js Interview Questions and Answers for freshers and Experienced. Basic and Advanced Node.js Questions. Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.</div>

<div class="ques-cls">Q:- What Is Node.js?</div>
<blockquote>Node.js is an open-source, cross-platform JavaScript runtime environment for executing JavaScript code on server side.</blockquote>

<div class="sub-head-cls">Most Popular JavaScript Engine:</div>
<ol>
    <li>
        Google Chrome - <b>V8</b> // Fastest JavaScript Engine 
    </li>
    <li>
        Mozilla FireFox - <b>SpiderMonkey</b>
    </li>
    <li>
        Microsoft Edge - <b>Chakra</b>
    </li>
</ol>

<div class="ques-cls">Q:- How to Install Node.js?</div>
<p>It's very easy to Install Node.js on Windows, Linux, etc</p>
<ul>
<li><a href="https://www.fullstacktutorials.com/tutorials/nodejs/install-nodejs-windows.html" target="_blank">Install Node.js on Windows</a></li>
<li><a href="https://www.fullstacktutorials.com/tutorials/nodejs/install-nodejs-linux.html" target="_blank">Install Node.js on Linux</a></li>
</ul>

<div class="ques-cls">
    Q:- In which Language Node.js is Written?
</div>
<p>
    Node.js is written in&lrm; - &lrm;C&lrm;, &lrm;C++&lrm;, &lrm;JavaScript and it's original author(s)&lrm; is &lrm;Ryan Dahl
</p>

<div class="ques-cls">Q:- What are the key features of Node.js?</div>
<p>Node.js has many features which makes node.js more powerful.</p>
<ol>
    <li>Asynchronous and Event Driven</li>
    <li>It's very fast</li>
    <li>Single Threaded & Highly Scalable</li>
    <li>Node.js library uses JavaScript</li>
    <li>NPM (Node Package Manager)</li>
    <li>No Buffering</li>
    <li>Community</li>
</ol>
<p>Let's see all above node.js feature details</p>
<ol>
    <li>
        <div class="sub-head-cls">Asynchronous and Event Driven:</div>
        <p>All APIs of Node.js library are asynchronous, that is, non-blocking I/O.</p>
		<p>Node.js can handle multiple concurrent request, it's the power of node.js. After it finish executing request it will run a callback to notify about its completion.</p>
    </li>
    <li>
        <div class="sub-head-cls">It's very fast:</div>
        <p>Node.js uses the Google Chrome's V8 JavaScript Runtime Engine written in C++, which compiles the JavaScript code into machine code, which makes node.js faster.</p>
		<p><b>JavaScript Engine:</b> It is a program that converts JavaScript's code into lower-level or machine code.</p>
    </li>
    <li>
        <div class="sub-head-cls">Single Threaded & Highly Scalable:</div>
        <p>Node.js is a single threaded, which in background (Under the hood Node.js uses many threads through libuv) uses multiple threads to execute asynchronous code.</p>
		<blockquote>Node.js applications uses <b>Single Threaded Event Loop Model</b> to handle multiple concurrent requests.</blockquote>
		<p>The Event Loop mechanism helps the server to respond in a non-blocking way, resulting in making the server highly scalable as opposed to traditional servers which create limited threads to handle requests.</p>
    </li>
    <li>
        <div class="sub-head-cls">Node.js library uses JavaScript:</div>
        <p>Since, majority of developers are already using JavaScript. so, development in Node.js becomes easier for a developer who already knows JavaScript.</p>
    </li>
    
    <li>
        <div class="sub-head-cls">NPM (Node Package Manager):</div>
        <p>NPM stands for Node Package Manager, it allows us to install various Packages for Node.js Application.</p>
    </li>
    <li>
        <div class="sub-head-cls">No Buffering:</div>
        <p>Node.js applications never buffer any data. They simply output the data in chunks.</p>
    </li>
	<li>
        <div class="sub-head-cls">Community:</div>
        <p>Node.js has a very good community which always keeps the framework updated with the latest trends in the web development.</p>
    </li>
</ol>

<div class="ques-cls">Q:- What is NPM? What is the need of NPM in Node.js?</div>
<p>
    <b>NPM</b> stands for <b>Node Package Manager</b>, it comes with node.js &amp; allows us to install various packages for Node.js Applications.
</p>
<pre class="code" data-language="javascript">
npm install express --save
npm install lodash --save
</pre>

<div class="ques-cls">
    Q:- What is "Callback Hell" and how can it be avoided?
</div>
<p>Callback hell refers to a coding pattern where there is a lot of nesting of callback functions. The code forms a pyramid-like structure and it becomes difficult to debug.</p>
<p>It is also called - <b>pyramid of doom</b></p>
<p>
    Just imagine if you need to make callback after callback:
</p>
<pre class="code" data-language="javascript">
getDetails(function(a){  
    getMoreDetails(a, function(b){
        getMoreDetails(b, function(c){ 
            getMoreDetails(c, function(d){ 
                getMoreDetails(d, function(e){ 
                    //and so on...
                });
            });
        });
    });
});
</pre>
<div class="sub-head-cls">
    Callback Hell can be avoided by using:
</div>
<ol>
    <li>
        Modularizing code
    </li>
    <li>
        using <a href="https://www.fullstacktutorials.com/tutorials/nodejs/nodejs-error-handling-using-promises.html" target="_blank">promise</a>
    </li>
    <li>
        using <a href="https://www.fullstacktutorials.com/tutorials/nodejs/nodejs-error-handling-using-async-await.html" target="_blank">async/await</a>
    </li>
</ol>

<div class="ques-cls">Q:- What is error first callback node.js?</div>
<ol>
    <li>
        The first argument of the callback is reserved for an error object. If an error occurred, it will be returned by the first 
        <b>
            err
        </b>
        argument.
    </li>
    <li>
        The second argument of the callback is reserved for any successful response data. If no error occurred, 
        <b>
            err
        </b>
        will be set to null and 
        <b>
            data
        </b>
        will be returned in the second argument.
    </li>
</ol>


<pre class="code" data-language="javascript">
fs.readFile('myfile.txt', function(err, data) {
// If an error occurred, handle it (throw etc)
if(err) {
	console.log('Error Found:' + err);
	throw err;
}
// Otherwise, log or process the data
console.log(data);
});
</pre>
<div class="ques-cls">
    Q:- Difference between package.json and package-lock.json?
</div>
<p>
    <b>
        package.json: 
    </b>
    The package.json is used for more than dependencies - like defining project properties, description, author &amp; license info etc.
</p>
<p>
    <b>
        package-lock.json: 
    </b>
    The package-lock.json is primarily used to lock dependencies to a specific version number.
</p>
<p>
    <i>
        If package-lock.json exists, it overrules package.json
    </i>
</p>
<div class="ques-cls">
    Q:- What is Modules in Node.js?
</div>
<p>This is mostly frequently asked Node.js Interview Questions.</p>
 Modules is a set of functionality or javascript libraries encapsulated into a single unit, which can be reused throughout the Node.js application.
<p>
    Each Node.js modules has it's own context.
</p>
<div class="sub-head-cls">
    Type of Modules in Node.js?
</div>
<ol>
    <li>
        <a href="https://www.fullstacktutorials.com/tutorials/nodejs/nodejs-core-modules.html" target="_blank">Core (In-built) Modules</a>
    </li>
    <li>
        <a href="https://www.fullstacktutorials.com/tutorials/nodejs/nodejs-local-modules.html" target="_blank">Local (User defined) Modules</a> 
    </li>
    <li>
        <a href="https://www.fullstacktutorials.com/tutorials/nodejs/nodejs-third-party-modules.html" target="_blank">3rd Party Modules</a>
    </li>
</ol>
<div class="sub-head-cls">
    1. Core Modules: 
</div>
<p>
    Node.js Core Modules comes with its Installation by default. You can use them as per application requirements
</p>

<div class="sub-head-cls">
    Include and Use Core Modules with Example:
</div>
<p>
    First you need to import core module using <b>require()</b> function.
</p>
<pre class="code" data-language="javascript">
const http = require('http');

var server = http.createServer(function(req, res){
	console.log("Congrats!, Node.js is running on Port 3000");
});

server.listen(3000); 
</pre>


<div class="sub-head-cls">
    2. Local Modules: 
</div>
<p>
    Local modules are <b>user defined modules</b> which are mainly used for specific projects and locally available in separate files or folders within project folders. 
</p>

<div class="sub-head-cls">
    Include and Use Local Module with Example:
</div>
<p>
    First you need to import core module using <b>require()</b> function.
</p>
<p>
    Create a folder common, inside common folder create a new file named utility.js with following code
</p>
<pre class="code">
//create module and export
function log(message) {  
	console.log(message);
}  
module.exports = log; 
</pre>

<p>
    Now, inside app.js or index.js file import that utility module using require() function.
</p>

<pre class="code">
//load/import module and use
const logger = require('./common/utility');   
var output = logger.log("Congrats!, You have successfully created a local module!");  
console.log(output); 
</pre>

<div class="sub-head-cls">
    3. 3rd Party Modules: 
</div>
<p>
    The 3rd party modules can be downloaded using NPM (Node Package Manager).
</p>

<div class="sub-head-cls">
    Include and Use 3rd Party Module with Example:
</div>
<pre class="code">
//Syntax
npm install -g module_name // Install Globally
npm install --save module_name //Install and save in package.json

//Install express module
npm install --save express  
npm install --save mongoose

//Install multiple modules at once
npm install --save express mongoose
</pre>

<p>for more detail - please read - <a href="https://www.fullstacktutorials.com/interviews/node-js-interview-questions-29.html" target="_blank">Node.js Interview Questions/Answers</a>