Before you bury yourself in packages, learn the Node.js runtime itself
Before you bury yourself in packages, learn the Node.js runtime itself

Before you bury yourself in packages, learn the Node.js runtime itself

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.

The Node.js knowledge challenge:

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!

  1. What is the relationship between Node.js and V8? Can Node work without V8?
  2. How come when you declare a global variable in any Node.js file it’s not really global to all modules?
  3. When exporting the API of a Node module, why can we sometimes use exports and other times we have to use module.exports?
  4. Can we require local files without using relative paths?
  5. Can different versions of the same package be used in the same application?
  6. What is the Event Loop? Is it part of V8?
  7. What is the Call Stack? Is it part of V8?
  8. What is the difference between setImmediate and process.nextTick?
  9. How do you make an asynchronous function return a value?
  10. Can callbacks be used with promises or is it one way or the other?
  11. What Node module is implemented by most other Node modules?
  12. What are the major differences between spawn, exec, and fork?
  13. How does the cluster module work? How is it different than using a load balancer?
  14. What are the --harmony-* flags?
  15. How can you read and inspect the memory usage of a Node.js process?
  16. What will Node do when both the call stack and the event loop queue are empty?
  17. What are V8 object and function templates?
  18. What is libuv and how does Node.js use it?
  19. How can you make Node’s REPL always use JavaScript strict mode?
  20. What is process.argv? What type of data does it hold?
  21. How can we do one final operation before a Node process exits? Can that operation be done asynchronously?
  22. What are some of the built-in dot commands that you can use in Node’s REPL?
  23. Besides V8 and libuv, what other external dependencies does Node have?
  24. What’s the problem with the process uncaughtException event? How is it different than the exit event?
  25. What does the _ mean inside of Node’s REPL?
  26. Do Node buffers use V8 memory? Can they be resized?
  27. What’s the difference between Buffer.alloc and Buffer.allocUnsafe?
  28. How is the slice method on buffers different from that on arrays?
  29. What is the string_decoder module useful for? How is it different than casting buffers to strings?
  30. What are the 5 major steps that the require function does?
  31. How can you check for the existence of a local module?
  32. What is the main property in package.json useful for?
  33. What are circular modular dependencies in Node and how can they be avoided?
  34. What are the 3 file extensions that will be automatically tried by the require function?
  35. When creating an http server and writing a response for a request, why is the end() function required?
  36. When is it ok to use the file system *Sync methods?
  37. How can you print only one level of a deeply nested object?
  38. What is the node-gyp package used for?
  39. The objects exports, require, and module are all globally available in every module but they are different in every module. How?
  40. If you execute a node script file that has the single line: console.log(arguments);, what exactly will node print?
  41. How can a module be both requirable by other modules and executable directly using the node command?
  42. What’s an example of a built-in stream in Node that is both readable and writable?
  43. What happens when the line cluster.fork() gets executed in a Node script?
  44. What’s the difference between using event emitters and using simple callback functions to allow for asynchronous handling of code?
  45. What is the console.time function useful for?
  46. What’s the difference between the Paused and the Flowing modes of readable streams?
  47. What does the --inspect argument do for the node command?
  48. How can you read data from a connected socket?
  49. The 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?
  50. When working with streams, when do you use the pipe function and when do you use events? Can those two methods be combined?

My take on the best way to learn the Node.js runtime

Learning Node.js can be challenging. Here are some of the guidelines that I hope will help along that journey:

Learn the good parts of JavaScript and learn its modern syntax (ES2015 and beyond)

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.

Understand the non-blocking nature of Node

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):

  1. Place your order | Give Node some instructions to execute (a function)
  2. Customize your order, no whipped cream for example | Give the function some arguments: ({whippedCream: false})
  3. Give the Starbucks worker your name with the order | Give Node a callback with your function: ({whippedCream: false}, callback)
  4. Step aside and the Starbucks worker will take orders from people who were after you in line | Node will take instructions from lines after yours.
  5. When your order is ready, the Starbucks worker will call your name and give you your order | When your function is computed and Node.js has a ready result for you, it’ll call your callback with that result: callback(result)

I’ve written a blog post about this: Asynchronous Programming as Seen at Starbucks

Learn the JavaScript concurrency model and how it is based on an event loop

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).

Understand how a Node process never sleeps, and will exit when there is nothing left to do

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 setIntervalfunction because that would create a permanent pending callback in the Event Loop.

Learn the global variables that you can use like process, module, and Buffer

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.

Learn what you can do with the built-in libraries that ship with Node and how they have a focus on “networking”

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.

Understand why Node is named Node

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.

Read and try to understand some code written for Node

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.

If you found this article helpful, please click the below. Follow me for more articles on Node.js and JavaScript.

Recommended Courses:

☞ ChatBots: Messenger ChatBot with API.AI and Node.JS

☞ Master the MEAN Stack – Learn By Example

☞ Build a Real Time web app in node.js , Angular.js, mongoDB

☞ Learn and Understand NodeJS

9 COMMENTS

  1. On July 1, 2017, the state of Georgia enacted a number of new
    legal guidelines; one lined medical marijuana Senate Bill sixteen expanded the medical circumstances eligible for remedy
    with low THC hashish oil. Lastly there’s a payment payable to the DMV for printing your Nevada Medical Marijuana Card as much as $thirteen. The doctor supporting your application isn’t licensed or not in good standing.
    If the Nevada State Well being Division has ever previously
    prohibited you from ever acquiring a Registry Identification Card.
    In case your utility for your Nevada Medical Marijuana Card is denied, you can re-apply after six (6)
    months have passed for the reason that date your utility was denied.

    If the appliance was only denied as a result of it was incomplete, you may re-apply immediately.
    Yes, a Las Vegas Marijuana Physician can suggest the use of medical marijuana for a
    minor beneath eighteen (18) who’s suffering from a
    qualifying medical situation.

    Once a Las Vegas Medical Marijuana physician and the state approve your
    caregiver, they can not act as a caregiver for another sufferers within the Nevada Medical Marijuana Card Program.
    You could get arrested If the police discover you
    or your caregiver in possession of marijuana without your Nevada
    Medical Marijuana Card. The case must be dropped should you can show you’ve got
    your Nevada Medical Marijuana Card and that you just had been following the State laws.
    Certainly one of our Las Vegas Medical Marijuana Doctors can help you develop into legal at the moment!
    Sure, as soon as a Las Vegas Medical Marijuana Doctor and the State approve your one (1) designated caregiver, that individual is the only one different then your self that can legally produce and possess your medical marijuana.

    Sure, the Nevada Board Of Medication cannot take disciplinary motion towards a doctor who recommends using cannabis medication for a
    affected person once a qualifying medical condition has been established.
    The sufferers home state exempts cardholders from prison prosecution for medical marijuana use.
    The regulation of the non-resident’s home state requires that physicians advise sufferers that medical marijuana use may
    help their symptoms as a precondition to the state issuing patients a medical marijuana card.
    A bit of fast background: You need a sound doctor’s recommendation to lawfully possess and grow marijuana in California.

    Rival service Meadow claims to Uber over a health care provider (mine actually came on a skateboard),
    however Eaze seems to be the primary dooby delivery startup
    in a position to hand out a authorized hashish card
    by means of EazeMD, its new video telephone convention service.
    You might want to truly ship a driver to deliver client goods, however there isn’t any motive you must have to really dispatch a health care provider
    and it really just streamlines the process,” founder Keith McCarty
    stated. California state legislation has traditionally required those wanting to get a state-issued hashish card to journey to a chosen clinic,
    pay $one hundred or more and then wait a few days for his or her card.

    None of them did, and Chase over at The Apothecarium
    stated that it was illegal for dispensaries to hook any physician up
    with potential patients for a marijuana card suggestion. Eaze would like us to make clear
    that it enables legally licensed third-occasion medical doctors
    to challenge the hashish card via EazeMD. In case this wasn’t clear within the article above, Eaze uses
    doctors to evaluate individuals who would possibly qualify for the usage of medical marijuana in the
    state of California. Overview Eaze is revolutionizing medical marijuana delivery as the world’s first
    app-based marketplace that connects verified patients to close
    by dispensaries.

    Along with the recommendation, a bonus function is offered that lets you register for a affected person ID in the type of a wallet-sized
    card. This authorities program assigns and registers
    a patient’s ID # to the California Division of Public Well being database and allows for both dispensaries and
    legislation enforcement to simply lookup a affected person and
    verify their paperwork. Although this can seem handy, by a public database, everybody – even employers –
    can potentially discover out that you use medical hashish.

    However don’t fret, through the use of sure online 420 evaluations
    services (we’ll counsel one quickly) that retains your information totally confidential, you’ll be able
    to still acquire access to dispensaries and safety from law
    enforcement, however below complete privateness.

    As mentioned earlier, this card is 100% legal and also
    comes with a patient ID#, but is completely confidential and grants sufferers the same entry to dispensaries and
    protection from law enforcement that a state-issued card does,
    however does not come with the public database attached. NuggMD provides online evaluations
    for first-time sufferers and those searching for
    to get a marijuana card renewal , so regardless of should you’re a newcomer or for those who’re a long-time patient, they can help you deal with
    all the pieces you want to get going! A medical marijuana card is valid
    for however lengthy your suggestion is, which is a whole year in the case of
    NuggMD.

    A patient should have a qualifying health condition to obtain a suggestion from a licensed doctor.
    As soon as a recommendation is received, the patient is required to be registered with the DPH to receive a
    medical marijuana card. In case you are a medical marijuana affected person in Massachusetts, you
    will be able to possess and buy up to 10 ounces of usable marijuana in a 60-day interval.
    Although the process to acquire a medical marijuana card in Massachusetts might be fairly tedious,
    patients can get the very best stage of experiential
    medical marijuana information and receive a suggestion from a health care provider in a number of quick steps.

    Notice that it’s essential to have proof of residency in the state of Massachusetts.
    There are a few medical necessities for patients to be qualified for a medical marijuana
    card in Massachusetts. In case you have a qualifying health situation and have received a certification from a
    licensed physician, you possibly can begin gathering
    documents to assist with the application course of together with a
    Massachusetts photo ID, photo of your self, and many others.
    As talked about earlier, a licensed doctor should certify patients before applying online.
    Current rules state that a affected person should
    have a bona fide physician-patient relationship or a bona fide advanced observe nurse-patient relationship” for a certification to be issued.

    Proof of this medical situation is required at the time of utility
    as well as a sound photo ID — and $200. He is at Canna Care immediately as a
    result of his card expired and he was unable to buy something from dispensaries final week.
    That is his first time at Canna Care; his physician, Jill Griffin, stopped certifying sufferers
    for medical marijuana earlier this month. The posters for lawyers appear
    a bit misplaced for a well being clinic, but
    with the use and possession of marijuana still a federal crime, they’re obligatory here.

    She notes that marijuana strains excessive in CBD, one of many active chemical substances in weed, will give patients the medical benefits of
    marijuana with out the excessive.

    In case you have not heard, THE MICHIGAN MARIJUANA LEGAL GUIDELINES HAVE CHANGED!
    Under are some links to the actual laws and the last one is a abstract of the laws by:
    The Home Fiscal Agency. The Michigan Medical Marihuana Program (MMMP) is a state registry program inside the Bureau of Health Professions at the Michigan Department of Licensing and Regulatory Affairs.
    The program will administer the Michigan Medical Marihuana Act as authorized by Michigan voters on November four,
    2008. Because of the rule changes, candidates who
    apply for registry cards on or after January 15, 2015 should use
    the new types which can be obtainable in the FAQ part of this web site.

    NHCC can’t name AZDHS on behalf of the patient and obtain data; that
    is the patient’s accountability. Within the event of a reputation or
    handle change, misplaced or stolen card, or change to
    caregiver information the patient should contact AZDHS by
    phone or email, or fill out a Card Change Application by
    means of NHCC can help the patient in filling out this application. If you are receiving Meals
    Stamp Advantages (SNAP) proper eligibility paperwork
    from DES (including active dates of advantages, affected person name, and
    date of beginning) must be offered to receive the discounted state
    fee. After fingerprints have been mailed, AZDHS is accountable for the approval
    of the application.

    Simply create an account, present solutions to some questions
    and rapidly chat with a licensed 420 doctor to get
    your cannibas card. Any sickness listed below that advantages from medical marijuana use and has
    been really useful and deemed acceptable by a
    licensed California doctor. In California, the qualifying circumstances to become a medical
    marijuana patient embrace: Migraines, Severe nausea, arthritis, glaucoma, persisten muscle
    spasms, continual ache, seizures, cachexia, AIDS, anorexia,
    most cancers, and any other continual or persistent medical sympton that substantially limites the ability of the person to conduct
    one or more main life activities or, if not alleviated, might trigger critical hurt to a patients safety, bodily, or psychological well being.

    Colorado has many accepted recreational marijuana stores,
    and a retail directory could be discovered here: Colorado recreational stores Most stores
    are open between eight AM and midnight, but Denver shops shut their
    doors at 7 PM. Residents of Colorado should purchase an ounce of marijuana however guests and tourists are restricted to 7 grams
    (1/4 ounce) per transaction. Travelers to the Colorado:
    Federal lands similar to nationwide parks, national
    forests, monuments and different authorities properties
    are not regulated by state legal guidelines. You might be cited for possession of marijuana and penalized as a federal offense.

    Sure, you don’t want a medical card to purchase weed
    in Colorado. You have to to make purchases at state licensed recreational stores
    fairly than medical dispensaries though. Seeds: seeds that can be used to develop
    your personal marijuana plants. Legal marijuana within the state
    of Colorado is going to be competitively priced.
    When you should not have a medical card, you cannot buy it from
    Medical Marijuana Dispensaries, but some Medical Marijuana Dispensaries
    may also sell Recreationally. No. All marijuana and marijuana merchandise purchased in Colorado must be consumed
    in Colorado. Nevertheless, at many public occasions in Colorado you will see residents overtly consuming in public as a form of
    civil disobedience.

    You have got federal raids on the locations where you can conveniently buy
    your medication, the governor of Arizona attempting to overturn in court her residents’ choice to institute a medical marijuana system,
    and Michigan’s legal professional normal making an attempt to make life
    as exhausting as he can for these using the system his state’s voters authorized by sixty three
    percent in 2008. And whereas it is not instantly the federal
    government’s fault, medical doctors are taking people off liver
    transplant ready lists for utilizing medical
    pot. It is not just that the federal government on both
    the federal and state level doesn’t need you to be able to legally and conveniently acquire
    your drugs, if that drugs is pot.

    As Independence Institute gun rights scholar David
    Kopel explains, some lower courts have determined that whereas the
    legal prohibition on felons owning handguns shouldn’t be
    inherently unreasonable or unconstitutional, the appliance of that
    regulation to felons of certain varieties—say, nonviolent ones
    in the distant past—isn’t all the time cheap. While the Wilson case as filed is difficult
    the very constitutionality of classifying drug users as outdoors the pale of
    the Second Amendment, Rainey can also be ready, he says, to argue
    extra narrowly that it is unreasonable to apply that category particularly to Wilson merely on the basis of her
    possessing the marijuana card.

    Mr. Perez stated investigators found plenty of long-standing and
    entrenched systemic deficiencies” that triggered or contributed to
    patterns of unlawful conduct, together with a failure to implement policies guiding deputies
    on lawful policing practices; permitting specialized models to engage in unconstitutional practices;
    insufficient training and supervision; an ineffective disciplinary,
    oversight and accountability system; and a lack of sufficient exterior oversight and accountability.
    Additionally FTA: The sources confirmed the city will get an opportunity to work with the Justice Division to handle the
    issues, or it’s going to face a federal lawsuit that would end in fines, penalties
    and even the appointment of an outdoor special master to oversee
    the Police Department.

    I’d much relatively have medical marijuana card holders with guns than individuals who misuse alcohol or stimulants.
    Talking of people with guns, i responded to a theft at a package retailer as we speak the
    place the man did not have his gun as a result of he went to church earlier than work, and didn’t
    like to depart his gun within the automobile.
    I am fairly sure that the process of obtaining a med mj card is just
    like acquiring a CWP: one must submit prints, a mug shot and a legitimate letter from a licensed medical skilled stating why you might want to use the drugs.
    You may belief that we preserve strict editorial
    integrity in our writing and assessments; nonetheless,
    we obtain compensation if you click on links to merchandise from our
    companions and get permitted. http://ipad-free-wallpapers.com/profile/roccorohde2.jpg

LEAVE A REPLY

Please enter your comment!
Please enter your name here