NodeJS Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is Node.js?
Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code server-side.
Example:
console.log('Hello, Node.js!');
Ques 2. What is npm?
npm (Node Package Manager) is the default package manager for Node.js, used to manage and distribute Node.js packages/modules.
Example:
npm install package_name
Ques 3. What is the purpose of package.json in Node.js?
package.json is a manifest file that contains metadata about a Node.js project, including dependencies, scripts, and other project-specific configurations.
Example:
npm init
Ques 4. What is the purpose of the 'global' object in Node.js?
The 'global' object represents the global scope in Node.js. Variables declared in the global scope are available globally to all modules in the application.
Example:
global.myVariable = 'Hello';
Ques 5. Explain the role of the 'require' function in Node.js.
The 'require' function is used to include modules in Node.js. It is the way to import functionality from other modules.
Example:
const fs = require('fs');
Ques 6. Explain the purpose of the 'os' module in Node.js.
The 'os' module provides a way to interact with the operating system. It includes methods to get information about the operating system like CPU architecture, memory, and network interfaces.
Example:
const os = require('os');
console.log(os.totalmem());
Ques 7. What is the purpose of the 'fs' module in Node.js?
The 'fs' module in Node.js provides file system-related functionality. It allows reading from and writing to files, manipulating file paths, and performing other file-related operations.
Example:
const fs = require('fs');
fs.readFile('file.txt', 'utf8', (err, data) => { /* handle file content */ });
Ques 8. What is the purpose of the 'url' module in Node.js?
The 'url' module provides utilities for URL resolution and parsing. It allows working with URLs, including parsing query parameters and formatting URL components.
Example:
const url = require('url');
const parsedUrl = url.parse('https://example.com/path?query=value', true);
Ques 9. Explain the purpose of the 'dotenv' module in Node.js.
The 'dotenv' module is used to load environment variables from a '.env' file into 'process.env'. It is commonly used in Node.js applications to manage configuration variables.
Example:
require('dotenv').config();
Most helpful rated by users: