No description, website, or topics provided.
Clone or download

General Assembly Logo

Introduction to the Node API


  • Familiarity with JavaScript as a language.
  • Familiarity with JavaScript in the browser environment.


By the end of this, developers should be able to:

  • Reference the Node API documentation for using JavaScript outside the browser.
  • Reference the MDN JavaScript documentation for JavaScript language features.
  • Write a Node script using the File System API.


  1. Fork and clone this repository. FAQ
  2. Create a new branch, training, for your work.
  3. Checkout to the training branch.
  4. Install dependencies with npm install.

JavaScript, the Browser, and Node

  • JavaScript: a language specified by ecma and implemented independently
  • Browser: an environment for running JavaScript (among other things)
  • Node: an environment for running JavaScript outside the browser

Somewhat valid equations:

Browser = JavaScript + Browser API + (other things)

Node = JavaScript + CLI/Server API

How are the two environments similar? How do they differ?

Lab: Research the File System API

Take a few minutes to read the following API documentation for the Node File System module.

While you're reading, imagine how you'd use each function. Write some example code in your notebook. Try to explain what each function does in your own words, including what sorts of arguments it takes and each argument's type.

If you finish early, look for other interesting functions in the File System module.

  1. fs.readdir(path, callback)
  2. fs.readdirSync(path)
  3. fs.readFile(file[, options], callback)
  4. fs.writeFile(file, data[, options], callback)


Use your developer skills to search for the relevant documentation on each of the following; are these methods part of JavaScript, Node, or the browser?

  • readline
  • Math.random
  • fs.readFile
  • http.ClientRequest
  • alert
  • new Promise

Demo: Simple Node Scripts

We'll start with a very simple Node script. The file bin/hello-world.js contains only one meaningful line of JavaScript. It prints "Hello world!" to the console. Normally, we would need a browser to execute this code. But if we run the file with node bin/hello-world.js, it executes the file outside of the browser.

This is what Node is for. It's a JavaScript runtime that isn't tied to a browser. It lets us write Javascript and execute it like we would Ruby or any other standard programming language.

We'll frequently need to pass some input into the scripts that we write with Node. Think about atom myFolderToOpen, for example. It's not written in Node, but it is essentially a script that accepts arguments from the command line. It wouldn't be very useful if we couldn't tell it what to open! When we open files or folders in atom we pass in a path to that file or folder as an argument. Let's takea look at bin/process-argv.js to get a sense of how arguments work in Node.

Because Node can run JavaScript on our local machines instead of the browser, it can do things like read files from the filesystem. Watch as I code out a simple script to read a file from the disk and print it to the console.

Annotate-Along: lib/copy-json.js

Code-Along: hey-yall.js

A more advanced "Hello, World!" with the Node file system API.

Write a Node.js command line function. It will take each line in from names.txt, and output "Hey " plus the name from each line one at a time to the command line.

Lab: Write a Randomizer

Randomize the lines in a file.

  1. Accept an input file and an output file name
  2. Shuffle the contents of the input file
  3. Create a new file to store the shuffled contents

How do you shuffle an array?

A common approach is called the Fisher-Yates Shuffle (aka the Knuth Shuffle). Install the knuth-shuffle package with npm install knuth-shuffle. Then use the knuth-shuffle package to complete this lab.

Run your code using: node bin/randomizer.js data/names.txt data/random_names.txt

Stretch Lab: Saving User Input

Write a Node script that prompts the user to enter some text into their terminal, then writes that text to a file. You will need to create a bin file and a lib file. The script should accept a filepath as an argument. This is where the text will be saved. The script should not use process.argv to accept that text from the user. Instead, you should research how to prompt the user for input in Node.js.

Additional Resources


  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact