What is Node? Intro to Node File System with JavaScript fs

Node is a JavaScript runtime environment that was first introduced in 2009 by Ryan Dahl, as a response to how slow web servers were at the time. In this lesson, we'll introduce Node and talk about the capability Node provides to make I/O tasks asynchronous and non-blocking.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL

Node is a JavaScript runtime environment that can run on different platforms. Mac, Windows, Linux. What this means is JavaScript which is originally created to run inside a web browser can now be run on any computer as a web server. Node was originally released in 2009 by Ryan Dahl as a response to how slow web servers were at the time. This is because most web servers would block the input output task which would lower throughputs. Node changed this model by making all input output tasks non-blocking and asynchronous. Non-blocking just means that a request from a certain interaction can be processed without waiting for the prior interaction request to finish. This allowed web servers to serve countless requests concurrently. Here's an example taken from the main Node website in comparing code between the synchronous blocking state and the asynchronous non-blocking state. This example covers the use of the Node file system which allows us to work with the file system in our computer. The file system module is included by stating require FS which is short for file system. The read file sync method is used to read files in the computer with which here we're stating that we want to read a file named file.md. The read file sync method is synchronous so here the more work function would only run after the entire file is read in the process above. This is an example of synchronous blocking code. The second line here read file sync blocks the code from continuing until its process is complete. As we've mentioned, node allows input output tasks to be non-blocking and asynchronous. So in this example, we're doing the exact same thing that is to say reading a file named filemd and running the more work function but we're doing so in a non-blocking setting. This is because we're now using a read file method which is asynchronous and allows the use of a callback function. This callback function won't run until the former function read file is complete. In this case read file doesn't block code since the more work function will be run while the file is being read. Whenever the reading of the file is complete, the callback function will then run. The ability to have the more work function be run alongside the reading of a file was a primary design choice in node to allow for higher throughputs. Callback functions, promises and the capability to use async/await are all the tools that node gives us to have input output tasks be asynchronous and non-blocking. If you've built any web application within a node environment, you've already come across something known as NPM. NPM short for node package manager is two things. It's an online repository for the publishing of open source node projects and it's a command line utility for interacting with the NPM repository. Now the ecosystem of third party tools that can be easily installed in a node application makes the node a very rich ecosystem. This also ties in with how we build applications in node by introducing just the tools and libraries we need in an app. One awesome benefit of using node is that node makes capable the building of universal JavaScript applications, often also known as isomorphic JavaScript. These are applications that have JavaScript both on the client and the server and this is exactly what we'll be doing in this course. Node is built against modern versions of V8, Google's JavaScript and Web Assembly engine, which helps ensure node stays up to date with the latest ECMA JavaScript syntax . To install node, all we have to do is go to nodejs.org and download the latest LTS version of node, which is recommended for most users. Once downloaded, we just follow the onscreen instructions to complete the installation. An easy way to verify if node is installed in your computer is to run the following command of the terminal, node-v, which will tell you the version of node installed. NPM is installed as part of node, but to also verify its availability, we can check for its version number with NPM-V. [ Silence ]