TechSchool
Home / Interview questions / Node.js

Node.js interview questions

45 questions about Node.js with short answers and code. Each answer links to a full lesson on this site. Free, with no signup and no ads.

Basics

1. What is Node.js?

Node.js is a JavaScript runtime. It runs JavaScript on the server side, not only in the browser. It uses the Chrome V8 engine.

Full lesson: Nodejs intro

2. How do you check the Node.js version on your computer?

Run the node -v command in a terminal. The command prints the installed version.

node -v

Full lesson: Nodejs tutorial

3. What is a function in Node.js?

A function is a reusable block of code. It performs one task. Functions help you reuse code and keep programs easy to maintain.

function greet(name) {
  console.log(`Hello, ${name}!`);
}

Full lesson: Nodejs function

4. How do you load the fs module in a Node.js file?

Call require with the name fs. The require call returns the module object. You can also use the import syntax in ES modules.

const fs = require('fs');

Full lesson: Nodejs fs module

5. How do you parse a URL string with the url module?

Call url.parse with the URL string. The method returns a URL object. The object holds the protocol, host, path, and query parts.

const url = require('url');
const u = url.parse('https://example.com:8080/a?x=1');
console.log(u.hostname);

Full lesson: Nodejs url module

6. How do you parse a query string into an object?

Call querystring.parse with the query string. The method returns a plain JavaScript object. Each key-value pair becomes one property.

const qs = require('querystring');
console.log(qs.parse('name=John&age=30'));

Full lesson: Nodejs querystring module

7. How do you get the path separator for the current system?

Use the path module. The path.sep property holds the separator. It is a backslash on Windows and a slash on Linux.

const path = require('path');
console.log(path.sep);

Full lesson: Nodejs path module

8. How do you read operating system information?

Use the os module. Methods such as os.arch and os.totalmem return system data. The data helps you adapt the program to the platform.

const os = require('os');
console.log(os.arch());
console.log(os.totalmem());

Full lesson: Nodejs os module

9. How do you create a basic HTTP server?

Call http.createServer with a request handler. Then call listen on a port. The server sends a response for each request.

const http = require('http');
http.createServer((req, res) => {
  res.end('Hello');
}).listen(8080);

Full lesson: Nodejs http module

10. How do you create a TCP server with the net module?

Call net.createServer with a connection handler. Then call listen on a port. The server runs a callback when a client connects.

const net = require('net');
net.createServer((socket) => {
  socket.end('Hello\n');
}).listen(8080);

Full lesson: Nodejs net module

11. How do you run a package without a global install?

Use npx and the package name. npx downloads the package and runs it. You do not install the package globally.

npx cowsay hello

Full lesson: Npx intro

12. How do you run JavaScript code in an isolated context?

Use the vm module. Call vm.runInNewContext with the code and a sandbox object. The code runs apart from the main program.

const vm = require('vm');
vm.runInNewContext('x = 1', {});

Full lesson: Nodejs vm module

13. What is a common mistake with the fs module?

A common mistake is to use the sync methods in a server. Sync calls block the event loop. Use the async methods or promises instead.

fs.readFile('a.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

Full lesson: Nodejs fs module

14. What is a common mistake with the http module?

A common mistake is to forget res.end. The response stays open and the client waits. Always end the response.

http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Done');
}).listen(8080);

Full lesson: Nodejs http module

15. What is a common mistake with the dns module?

A common mistake is to ignore the error argument in the callback. A failed lookup returns an error. Always check the error first.

dns.lookup('example.com', (err, address) => {
  if (err) return console.error(err.message);
  console.log(address);
});

Full lesson: Nodejs dns module

Intermediate

16. What is the Node.js REPL?

The REPL is an interactive programming environment. It reads, evaluates, prints, and loops code you type.

node

Full lesson: Nodejs repl module

17. How do you create a custom event with the events module?

You create an EventEmitter object. You register a listener with the on method and trigger the event with the emit method.

const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('greet', () => console.log('Hello'));
emitter.emit('greet');

Full lesson: Nodejs events module

18. How do you compress data with the zlib module?

You use the gzip function to compress a buffer. The callback receives the compressed data.

const zlib = require('zlib');
zlib.gzip('Hello World', (err, buffer) => {
  if (err) throw err;
  console.log(buffer);
});

Full lesson: Nodejs zlib module

19. How do you create a UDP socket with the dgram module?

You call dgram.createSocket with the type 'udp4'. The socket can send and receive datagrams.

const dgram = require('dgram');
const socket = dgram.createSocket('udp4');
socket.send('Hello', 8080, 'localhost');

Full lesson: Nodejs dgram module

20. How do you create a Buffer from a string?

You use Buffer.from with the string and an encoding. The default encoding is utf8.

const buf = Buffer.from('Hello', 'utf8');
console.log(buf);

Full lesson: Nodejs buffer module

21. How do you create a SHA-256 hash with the crypto module?

You use crypto.createHash with the algorithm name. You update the hash with data and get the digest in hex format.

const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update('Hello World');
console.log(hash.digest('hex'));

Full lesson: Nodejs crypto module

22. How do you format a string with the util module?

You use util.format with a format string and arguments. It returns a formatted string.

const util = require('util');
console.log(util.format('%s is %d', 'Alice', 30));

Full lesson: Nodejs util module

23. How do you write a basic assertion with the assert module?

You use assert.ok to check if a value is truthy. If the value is falsy, it throws an AssertionError.

const assert = require('assert');
assert.ok(true, 'This passes');
assert.ok(false, 'This fails');

Full lesson: Nodejs assert module

24. What is the event loop in Node.js?

The event loop is a mechanism that handles asynchronous callbacks. It allows Node.js to perform non-blocking I/O operations.

Full lesson: Nodejs event loop

25. How do you create a web server with the http module?

You use http.createServer with a request handler. The server listens on a port and responds to requests.

const http = require('http');
const server = http.createServer((req, res) => {
  res.end('Hello World');
});
server.listen(3000);

Full lesson: Nodejs web module

26. What is a common mistake when using the events module?

A common mistake is to emit an event before adding a listener. The event is lost and the listener never runs.

const emitter = new EventEmitter();
emitter.emit('greet'); // No listener yet
emitter.on('greet', () => console.log('Hello')); // Never called

Full lesson: Nodejs events module

27. What is a common mistake with the path module?

A common mistake is to use string concatenation for paths. This fails on different operating systems. Use path.join instead.

const path = require('path');
// Wrong: 'folder' + '/' + 'file.txt'
// Right: path.join('folder', 'file.txt')

Full lesson: Nodejs path module

28. What is a common mistake with the Buffer module?

A common mistake is to assume Buffer memory is zero-filled. Buffer.allocUnsafe returns uninitialized memory. Use Buffer.alloc for safety.

const buf = Buffer.alloc(5); // safe, zero-filled
console.log(buf);

Full lesson: Nodejs buffer module

29. What is a common mistake with the crypto module?

A common mistake is to reuse a hash object after calling digest. The hash is finalized and cannot be updated again. Create a new hash for each operation.

const hash = crypto.createHash('sha256');
hash.update('data');
hash.digest('hex');
// hash.update('more'); // Error

Full lesson: Nodejs crypto module

30. What is a common mistake with the domain module?

A common mistake is to use the domain module in new code. The module is deprecated. Use async/await or try/catch instead.

Full lesson: Nodejs domain module

Common mistakes

31. What is the events module in Node.js?

The events module provides the EventEmitter class. This class lets objects create, trigger, and listen to custom events.

const EventEmitter = require('events');

Full lesson: Nodejs events module

32. How do you create a Buffer with a fixed size?

Call Buffer.alloc with the number of bytes. The buffer is filled with zeros by default.

const buf = Buffer.alloc(10);
console.log(buf.length); // 10

Full lesson: Nodejs buffer module

33. How do you convert a Buffer to a string?

Call the toString method on the buffer. You can pass an encoding such as 'utf8'.

const buf = Buffer.from('Hello');
console.log(buf.toString()); // Hello

Full lesson: Nodejs buffer module

34. How do you get the current working directory with the path module?

Use path.resolve() with no arguments. It returns the absolute path of the current working directory.

const path = require('path');
console.log(path.resolve());

Full lesson: Nodejs path module

35. How do you join two path segments safely?

Use the path.join method. It joins segments and normalizes the result for the current system.

const path = require('path');
console.log(path.join('folder', 'file.txt'));

Full lesson: Nodejs path module

36. How do you inspect an object with the util module?

Call util.inspect on the object. It returns a string that is easy to read.

const util = require('util');
console.log(util.inspect({ a: 1 }));

Full lesson: Nodejs util module

37. How do you resolve a hostname to an IP address with the dns module?

Call dns.lookup with the hostname and a callback. The callback receives an error and the address.

const dns = require('dns');
dns.lookup('example.com', (err, address) => {
  console.log(address);
});

Full lesson: Nodejs dns module

38. How do you create a hash object with the crypto module?

Call crypto.createHash with an algorithm name. Then call update with the data and digest to get the result.

const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update('Hello');
console.log(hash.digest('hex'));

Full lesson: Nodejs crypto module

39. How do you start a basic REPL session?

Run the node command with no file argument. Node.js starts the REPL and shows a prompt.

node

Full lesson: Nodejs repl module

40. What is a common mistake with the util module?

A common mistake is to use util.inspect output as data. The output is for display only and can change between versions.

Full lesson: Nodejs util module

41. What is a common mistake with the zlib module?

A common mistake is to forget that zlib methods are asynchronous by default. You must wait for the callback or use the sync version.

const zlib = require('zlib');
zlib.gzip('data', (err, buf) => {
  if (err) throw err;
  console.log(buf.length);
});

Full lesson: Nodejs zlib module

42. What is a common mistake with the net module?

A common mistake is to ignore the 'error' event on a socket. An unhandled error event stops the process.

const net = require('net');
const server = net.createServer();
server.on('error', (err) => console.error(err));

Full lesson: Nodejs net module

43. What is a common mistake with the assert module?

A common mistake is to use assert for user input checks in production. Assertion errors stop the program and are for testing only.

Full lesson: Nodejs assert module

44. What is a common mistake with the vm module?

A common mistake is to run untrusted code in a vm context. The vm module does not provide a security sandbox.

Full lesson: Nodejs vm module

45. What is a common mistake with the querystring module?

A common mistake is to expect nested objects from querystring.parse. The querystring module does not support nested structures.

const qs = require('querystring');
console.log(qs.parse('a=1&b=2')); // { a: '1', b: '2' }

Full lesson: Nodejs querystring module

More on TechSchool
See the Node.js cheat sheet Python JavaScript TypeScript HTML CSS React Vue 3 Java PHP C C++ C# Go SQL MongoDB Redis Linux Git Docker Data Structures