Node.js / NestJS

Node.js Core APIs

fs, path, events, process, os, streams, buffers, child_process, worker threads

25 interview questionsยท
Junior
1

Which method from the fs module allows reading a file asynchronously?

Answer

fs.readFile() reads the entire file asynchronously and returns its content in a callback. The fs.read() method exists but works with file descriptors for partial reads. fs.readFileSync() is the synchronous version. This asynchronous API avoids blocking the event loop during expensive I/O operations.

2

What is the main difference between path.join() and path.resolve()?

Answer

path.join() simply concatenates path segments, while path.resolve() resolves paths into an absolute path from the current directory. For example, path.join('a', 'b') returns 'a/b', while path.resolve('a', 'b') returns a complete absolute path like /current/dir/a/b. Use path.resolve() to get guaranteed absolute paths.

3

Which flag to use with fs.writeFile() to append content without overwriting the file?

Answer

The 'a' flag (append) opens the file in append mode, preserving existing content and adding to the end. The default 'w' flag (write) overwrites the file. Other flags exist like 'r' (read), 'wx' (write exclusive), etc. Flags are inherited from the POSIX system and provide fine control over file opening behavior.

4

Which method from the path module normalizes a path by resolving . and .. segments?

5

How to create a directory and all its missing parents with fs?

+22 interview questions

Master Node.js / NestJS for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free