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?
Node is the environment which requires installation, while Angular is the framework which can be added by stroke in code. That's why Angular is used mostly in front-end and web-apps development, while Node is better for back-end and hard-weight applications. And as Angular is the framework it uses only JavaScript, while Node has components written on C++ together with C and JavaScript.
4. What differences are between JavaScript and Node.js?
JavaScript is more versatile as it can be run in any modern browser, while Node.js requires installation on the user's machine. But at the same time Node.js can launch JavaScript outside the browser, which in some cases is more comfortable. For example, it helps in developing server-side parts (back-end), while JavaScript is good for client-side (front-end) development. Also, as JS scripts execute in the browser it can work with DOM and HTML, which is unavailable in 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?
REPL is the abbreviation of Read-Eval-Print-Lopp, which is an interactive shell that processes expressions Node.js. Its name shows how it works: 1 step - read user's code, 2 step - evaluate the result of interpreting, 3 step - print the result of previous step and 4 step - back to step 1. In Node.js it is mostly for testing JavaScript code "on the fly".
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?
It initializes virtual machines based on the V8 JavaScript engine, where the programming language is JavaScript. It works on asynchronous principle with single thread event loop execution. Thankfully it is spared from code block I/O problems. Also it has an HTTP module so it can work as a standalone web server.
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.