Mini Series Of NodeJS Interview Question
1. What is npm?
This is a packages manager used in the Node.js environment. As a rule this software is installed on the user’s PC together with other parts of Node.js.
2. Is it free to use Node.js?
Yes, it’s a free and open-source project available on Github.
3. What differences are between Angular.js and Node.js?
5. What are the advantages and disadvantages of using Node.js?
Among Node.js advantages most valuable are: easy scalability, comfortable environment for full stack development and active community support. In technical aspects worth mentioning event-based model, asynchronous requests handling and built-in JSON support.
As key disadvantages of Node.js community describes: bad optimization for heavy tasks, frequent changes into actual API, problems with documentation and things called “callback hell” which appear as result of asynchronous execution nature.
6. How to work with directories in the Node.js?
Module fs which realize interaction in Node.js allow sync and async operations with folders. To create and delete files in synchronous mode there are fs.mkdirSync() and fs.unlinkSync() methods, while in async these can be done with fs.mkdir() and fs.unlink(). To get a list of files in a folder can be used synchronous method fs.readdirSync() or asynchronous method fs.readdir().
7. How to work with files in in Node.js?
In Node.js input/output operations are realized in the fs module, which support synchronous and asynchronous operations. Key commands for sync interaction with files:
fs.writeFileSync(file, data, option) – create “file” with “data”
fs.unlinkSync (path) – delete file located on “path”
fs.readFileSync (path, option) – open and read file on “path”
Key commands for async interaction with files:
fs.writeFile(file, data, options, callback) – creation “file” with “name”
fs.unlink(path, callback) – file deletion located on “path”
fs.readFile(path, option) – open and read file on “path”
8. What are the streams in Node.js?
Streams in Node.js are used for sequential reading and writing data in files. It is most usable for working with large files, as streams don’t open the whole file, which is good for device memory usage. Instead of this, it reads (or writes) files step-by-step according to part of the file requested by the developer.
9. What is callback hell in Node.js?
As Node.js supports asynchronous operations, there can appear issues with callbacks execution orders. Developers who use complex callbacks must predict how to detect and avoid situations when script launches an async callback which requires data from other callbacks which doesn’t finished execution yet.
10. What is the difference between dependencies and devDependencies in Node.js?
Both are a kind of package used for development. Packages required for application working after release must be installed as a – “dependencies“, while packages required only for development or testing as a – “devDependencies“. The last one wouldn’t be included in the product release.
11. How are ‘Child Threads’ handled in Node.js?
Child threads in Node.js can be handled with clild_process and worker_threads modules. The first one allows creating a child process inside the node, while the worker_threads can be used for parallel threads organization.
12 Is Node a single threaded application?
Yes, because it was developed for asynchronous operations, which as expected doesn’t require multiple threads.
13 What is REPL in the context of Node.js?
14. What are the event listeners in Node.js?
Event listeners in Node.js are the part of the “Events” module which reacts when specified “event” occurs according to data in EventEmitter. Event listeners can show a specified message or launch some function if an event has happened and must be deleted manually as it always stays in memory up to the end of program execution.
15. Difference between the cluster and child_process modules?
Cluster can create processes with one main process which routes requests inside the cluster. This and other instruments in this module allow forking processes for scaling systems. Instead of this child_process module create processes which can interact between each other and with the system.
16. What is the purpose of the process object in Node.js?
It is a global object which can be accessed from anywhere without requiring it.
17. How does Node.js prevent blocking code?
It is based on a single-threaded event loop, in other words Node.js executes code in asynchronous mode which helps prevent these cases. It completes all operations according to the queue in the stack, but when some of them require more time it gets the parallel process and executes simultaneously with the main process.
18. What is the Reactor Pattern in Node.js?
It is a pattern of execution input and output operations, which work asynchronously and helps to avoid blocking code moments. The main reactor principles include: simultaneous access to resources, event notifier for managing access, events loop for managing queue and request handler.
19. What is the difference between setImmediate() vs setTimeout() in Node.js?
The setTimeout() will run the callback when the threshold in ms will be elapsed, while setImmediate() executes the callback after all I/O events are processed.
20. What is the difference between fork() and spawn() methods in Node.js?
The spawn() function creates a child process which automatically sends data to the parent process, without creating a new V8 instance. The fork() function also creates a new child process but with a new V8 instance, so its processes can work asynchronously inside the parent’s node. That’s why fork() is often used for organizing multi-tasking inside one parent’s node, while spawn() just to add a new child process in the queue.
21. What is EventEmitter in Node.js?
EventEmitter is a class which provides interaction with events, especially their creation.
22. How do you manage packages in your Node.js project?
Node.js has a built-in instrument named Node Package Manager (NPM). It is a command-line tool which can install packages from local storage or from a website. Also it can show the list of installed packages and update them if required.
23. What is the fork() in Node JS?
This is a function which creates a child process, connected to the main process during code execution. In practice it is used for creating multi-tasking inside threads.
24. How does Node.js work?
25. What is WASI and why is it being introduced?
WASI it’s a WebAssembly System Interface which allows access to OS for sandboxed WebAssembly apps. It provides decisions for safe code execution beyond the browser.