DEV Community

loading...

Node.js Interview Questions Answers - Basic + Advanced

Full Stack Tutorials
Full Stack Developer (JavaScript, MERN Stack, PHP, Python, SQL etc). Author@fullstacktutorials.com - Learn Free Online Tutorials
・6 min read

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.

Q:- What Is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment for executing JavaScript code on server side.

Most Popular JavaScript Engine:

  1. Google Chrome - V8 // Fastest JavaScript Engine
  2. Mozilla FireFox - SpiderMonkey
  3. Microsoft Edge - Chakra

Q:- How to Install Node.js?

It's very easy to Install Node.js on Windows, Linux, etc

Q:- In which Language Node.js is Written?

Node.js is written in‎ - ‎C‎, ‎C++‎, ‎JavaScript and it's original author(s)‎ is ‎Ryan Dahl

Q:- What are the key features of Node.js?

Node.js has many features which makes node.js more powerful.

  1. Asynchronous and Event Driven
  2. It's very fast
  3. Single Threaded & Highly Scalable
  4. Node.js library uses JavaScript
  5. NPM (Node Package Manager)
  6. No Buffering
  7. Community

Let's see all above node.js feature details

  1. Asynchronous and Event Driven:

    All APIs of Node.js library are asynchronous, that is, non-blocking I/O.

    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.

  2. It's very fast:

    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.

    JavaScript Engine: It is a program that converts JavaScript's code into lower-level or machine code.

  3. Single Threaded & Highly Scalable:

    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.

    Node.js applications uses Single Threaded Event Loop Model to handle multiple concurrent requests.

    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.

  4. Node.js library uses JavaScript:

    Since, majority of developers are already using JavaScript. so, development in Node.js becomes easier for a developer who already knows JavaScript.

  5. NPM (Node Package Manager):

    NPM stands for Node Package Manager, it allows us to install various Packages for Node.js Application.

  6. No Buffering:

    Node.js applications never buffer any data. They simply output the data in chunks.

  7. Community:

    Node.js has a very good community which always keeps the framework updated with the latest trends in the web development.

Q:- What is NPM? What is the need of NPM in Node.js?

NPM stands for Node Package Manager, it comes with node.js & allows us to install various packages for Node.js Applications.

npm install express --save
npm install lodash --save
Q:- What is "Callback Hell" and how can it be avoided?

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.

It is also called - pyramid of doom

Just imagine if you need to make callback after callback:

getDetails(function(a){  
    getMoreDetails(a, function(b){
        getMoreDetails(b, function(c){ 
            getMoreDetails(c, function(d){ 
                getMoreDetails(d, function(e){ 
                    //and so on...
                });
            });
        });
    });
});
Callback Hell can be avoided by using:
  1. Modularizing code
  2. using promise
  3. using async/await

Q:- What is error first callback node.js?

  1. The first argument of the callback is reserved for an error object. If an error occurred, it will be returned by the first err argument.
  2. The second argument of the callback is reserved for any successful response data. If no error occurred, err will be set to null and data will be returned in the second argument.
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);
});
Q:- Difference between package.json and package-lock.json?

package.json: The package.json is used for more than dependencies - like defining project properties, description, author & license info etc.

package-lock.json: The package-lock.json is primarily used to lock dependencies to a specific version number.

If package-lock.json exists, it overrules package.json

Q:- What is Modules in Node.js?

This is mostly frequently asked Node.js Interview Questions.

Modules is a set of functionality or javascript libraries encapsulated into a single unit, which can be reused throughout the Node.js application.

Each Node.js modules has it's own context.

Type of Modules in Node.js?
  1. Core (In-built) Modules
  2. Local (User defined) Modules
  3. 3rd Party Modules
1. Core Modules: 

Node.js Core Modules comes with its Installation by default. You can use them as per application requirements

Include and Use Core Modules with Example:

First you need to import core module using require() function.

const http = require('http');

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

server.listen(3000); 
2. Local Modules: 

Local modules are user defined modules which are mainly used for specific projects and locally available in separate files or folders within project folders.

Include and Use Local Module with Example:

First you need to import core module using require() function.

Create a folder common, inside common folder create a new file named utility.js with following code

//create module and export
function log(message) {  
    console.log(message);
}  
module.exports = log; 

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

//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); 
3. 3rd Party Modules: 

The 3rd party modules can be downloaded using NPM (Node Package Manager).

Include and Use 3rd Party Module with Example:
//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

for more detail - please read - Node.js Interview Questions/Answers

Discussion (1)

Collapse
fullstacktuts profile image
Full Stack Tutorials Author

Thanks all