This article will challenge the very limits of your Node.js knowledge.
I started learning Node.js shortly after Ryan Dahl first presented it, and I wasn’t able to answer a lot of the questions I ask in this article even a year ago. If you can truly answer all of these questions, then your knowledge of Node.js is beyond great. We should be friends.
The reason I think this challenge will take you by surprise is that many of us have been mostly learning Node the wrong way. Most tutorials, books, and courses about Node focus on the Node ecosystem — not the Node runtime itself. They focus on teaching what can be done with all the packages available for you when you work with Node, like Express and Socket.IO, rather than teaching the capabilities of the Node runtime itself.
There are good reasons for this. Node is raw and flexible. It doesn’t provide complete solutions, but rather provides a rich runtime that enables you to implement solutions of your own. Libraries like Express.js and Socket.IO are more of complete solutions, so it makes more sense to teach those libraries, so you can enable learners to use these complete solutions.
The conventional wisdom seems to be that only those whose job is to write libraries like Express.js and Socket.IO need to understand everything about the Node.js runtime. But I think this is wrong. A solid understanding of the Node.js runtime itself is the best thing you can do before using those complete solutions. You should at least have the knowledge and confidence to judge a package by its code, so you can make an educated decision about using it.
This is why I decided to create a Pluralsight course fully dedicated to pure Node. While doing the research for the course, I put together a list of specific questions for you to determine whether your knowledge of the Node runtime is already strong enough, or if it could be improved.
If you can answer most of these questions and you’re looking for a job, let me know! If on the other hand, most of these questions take you by surprise, you just need to make learning the Node runtime itself a priority. Your knowledge of that will make you a much more desirable developer.
Some of these questions are short and easy while others require longer answers and deeper knowledge. They are all presented here in no particular order.
I know that you’re going to want answers after reading this list. The advice section below has some answers, but I’ll also be answering all of these questions in a series of freeCodeCamp articles after this one. But let me tease your knowledge first!
exports
and other times we have to use module.exports
?setImmediate
and process.nextTick
?spawn
, exec
, and fork
?--harmony-*
flags?process.argv
? What type of data does it hold?uncaughtException
event? How is it different than the exit
event?_
mean inside of Node’s REPL?Buffer.alloc
and Buffer.allocUnsafe
?slice
method on buffers different from that on arrays?string_decoder
module useful for? How is it different than casting buffers to strings?main
property in package.json
useful for?end()
function required?*Sync
methods?node-gyp
package used for?exports
, require
, and module
are all globally available in every module but they are different in every module. How?console.log(arguments);
, what exactly will node print?node
command?console.time
function useful for?--inspect
argument do for the node command?require
function always caches the module it requires. What can you do if you need to execute the code in a required module many times?Learning Node.js can be challenging. Here are some of the guidelines that I hope will help along that journey:
Node is a set of libraries on top of a VM engine which can compile JavaScript so it goes without saying that the important skills for JavaScript itself is a subset of the important skills for Node. You should start with JavaScript itself.
Do you understand functions, scopes, binding, this keyword, new keyword, closures, classes, module patterns, prototypes, callbacks, and promises? Are you aware of the various methods that can be used on Numbers, Strings, Arrays, Sets, Objects, and Maps? Getting yourself comfortable with the items on this list will make learning the Node API much easier. For example, trying to learn the ‘fs’ module methods before you have a good understanding of callbacks may lead to unnecessary confusion.
Callbacks and promises (and generators/async patterns) are especially important for Node. You need to understand how asynchronous operations are first class in Node.
You can compare the non-blocking nature of lines of code in a Node program to the way you order a Starbucks coffee (in the store, not the drive-thru):
({whippedCream: false})
({whippedCream: false}, callback)
callback(result)
I’ve written a blog post about this: Asynchronous Programming as Seen at Starbucks
There is a Stack, a Heap, and a Queue. You can read books on this subject and still not understand it completely, but I guarantee you’ll do if you watch this guy.
Philip explains the Event Loop that’s in the browser, but almost the exact same thing applies to Node.js (there are some differences).
A Node process can be idle but it never sleeps. It keeps track of all the callbacks that are pending and if there is nothing left to execute it will simply exit. To keep a Node process running you can for example use a setInterval
function because that would create a permanent pending callback in the Event Loop.
They’re all defined on a global variable (which is usually compared to the window
variable in browsers). In a Node’s REPL, type global.
and hit tab to see all the items available (or simple double-tab on an empty line). Some of these items are JavaScript structures (like Array
and Object
). Some of them are Node library functions (like setTimeout
, or console
to print to stdout
/stderr
), and some of them are Node global objects that you can use for certain tasks (for example, process.env
can be used to read the host environment variables).
You need to understand most of what you see in that list.
Some of those will feel familiar, like Timers for example, because they also exist in the browser and Node is simulating that environment. However, there is much more to learn, like fs
, path
, readline
, http
, net
, stream
, cluster
, … (The auto-complete list above has them all).
For example, you can read/write files with fs
, you can run a streaming-ready web server using “http
”, and you can run a tcp server and program sockets with “net
”. Node today is so much more powerful than it was just a year ago, and it’s getting better by the commit. Before you look for a package to do some task, make sure that you can’t do that task with the built-in Node packages first.
The events
library is especially important because most of Node architecture is event-driven.
There’s always more that you can learn about the Node API, so keep expanding your horizons.
You build simple single-process building blocks (nodes) that can be organized with good networking protocols to have them communicate with each other and scale up to build large distributed programs. Scaling a Node application is not an afterthought — it’s built right into the name.
Pick a framework, like Express, and try to understand some of its code. Ask specific questions about the things you don’t understand. I try to answer questions on the jsComplete slack channel when I can.
Finally, write a web application in Node without using any frameworks. Try to handle as many cases as you can, respond with an HTML file, parse query strings, accept form input, and create an endpoint that responds with JSON.
Also try writing a chat server, publishing an npm package, and contributing to an open-source Node-based project.
Good luck!
If you found this article helpful, please click the💚 below. Follow me for more articles on Node.js and JavaScript.
I create online courses for Pluralsight and Lynda. My most recent courses are Advanced React.js, Advanced Node.js, and Learning Full-stack JavaScript.
☞ JavaScript Programming Tutorial Full Course for Beginners
☞ Learn JavaScript - Become a Zero to Hero
☞ Machine Learning Zero to Hero - Learn Machine Learning from scratch
☞ Top 4 Programming Languages to Learn In 2019