You don’t know Node


The problem: most educational content about Nodejs focuses on Node packages and not its runtime.

I picked a few questions and answers to feature for this article. They are presented in headers below. Try to answer them in your head first!

Question #1: What is the Call Stack and is it part of V8?

The Call Stack is definitely part of V8. It is the data structure that V8 uses to keep track of function invocations. Every time we invoke a function, V8 places a reference to that function on the call stack and it keeps doing so for each nested invocation of other functions. This also includes functions that call themselves recursively.

Screenshot captured from my Pluralsight course — Advanced Node.js

Question #2: What is the Event Loop? Is it part of V8?

Where do you think the event loop is in this diagram?

Screenshot captured from my Pluralsight course — Advanced Node.js
Screenshot captured from my Pluralsight course — Advanced Node.js

Question #3: What will Node do when the Call Stack and the event loop queues are all empty?

It will simply exit.

Question #4: Besides V8 and Libuv, what other external dependencies does Node have?

The following are all separate libraries that a Node process can use:

  • c-ares
  • OpenSSL
  • zlib

Question #5: Would it be possible to run a Node process without V8?

This might be a trick question. You do need a VM to run a Node process, but V8 is not the only VM that you can use. You can use Chakra.

Question #6: What is the difference between module.exports and exports?

You can always use module.exports to export the API of your modules. You can also use exports except for one case:

module.exports.g = ...  // Okexports.g = ...         // Okmodule.exports = ...    // Okexports = ...           // Not Ok

Question #7: How come top-level variables are not global?

If you have module1 that defines a top-level variable g:

// module1.jsvar g = 42;
// script.jsconsole.log(arguments);

Question #8: The objects exports, require, and module are all globally available in every file but they are different in every file. How?

When you need to use the require object, you just use it directly as if it was a global variable. However, if you inspect require in two different files, you will see two different objects. How?

Question #9: What are circular module dependencies in Node?

If you have a module1 that requires module2 and module2 that in-turn requires module1, what is going to happen? An error?

// module1require('./module2');
// module2require('./module1');

Question #10: When is it OK to use the file system *Sync methods (like readFileSync)

Every fs method in Node has a synchronous version. Why would you use a sync method instead of the async one?


samerbuna.com

jsComplete EdgeCoders

We write about the new and leading edge technologies with a focus on JavaScript

Samer Buna

Written by

Author for Pluralsight, O'Reilly, Manning, and LinkedIn Learning. Curator of jsComplete.com

jsComplete EdgeCoders

We write about the new and leading edge technologies with a focus on JavaScript