Modules, Node.js & npm: Working Like a Pro

Split your code into files, run it anywhere, and borrow from millions of packages. Eleven short pages from Junior Year of College, about 17 minutes of reading in all.

What you’ll learn

  • Why big programs are split into modules
  • export and import
  • Using modules in the browser
  • What Node.js can do that browsers can't
  • Reading and writing files with Node
  • Command-line arguments with process.argv
  • npm, package.json and installing packages
  • Testing your code with Node's built-in test runner
  • A tour of the professional developer's toolbox

The pages

Eleven pages, about 17 minutes.

  1. Why Modules? Imagine a kitchen with no cupboards, drawers or shelves. Every pot, spoon, plate, spice jar and tea towel is just…
  2. Exporting and Importing Let's make a module. Create a file called math-utils.js. The keyword export in front of a variable or function means…
  3. Modules in the Browser To use modules in a web page, add type="module" to your script tag. Module scripts behave a little differently from…
  4. Node.js: JavaScript Outside the Browser You've been using Node.js since Kindergarten to run JavaScript files. But Node can do much more than print to the…
  5. Reading and Writing Files Node comes with a bunch of built-in modules. You import them using a name that starts with node:. The one you'll use…
  6. Command-Line Arguments When you run a Node program, you can pass extra words after the file name. Node puts them in an array called…
  7. npm: The World's Biggest Toolbox Here's one of the best things about JavaScript: you almost never have to build everything yourself. Developers all…
  8. Testing Your Code How do you know your code works? So far, you've been running it and eyeballing the output. That's fine for small…
  9. Mini Project: The Expense Tracker CLI Let's build a real command-line tool that tracks your spending and saves it to a file. Make a new folder, run npm init…
  10. The Professional Developer's Toolbox You now know JavaScript itself. But professional developers also use a set of tools around the language. You don't…
  11. Recap The key ideas of Junior Year, JavaScript modules and Node.js, on one page. Modules split code into separate files…

Reading is step one

CurlyScript turns these notes into a game: short questions, an explanation after every answer, and a test at the end of each grade. Free, with nothing to sign up for.

Start learning