Start here

Using the Playground

The playground/ folder is your workspace, published on GitHub as coder-hop/nodejs-book-playground. Exercises marked playground ask you to create files there, and together they grow into the Chapter 16 project. This page shows how to set it up, run files, use watch mode and the REPL, run tests, and fix the most common problems.

On this page

  • Installing Node and checking the version.
  • The playground layout and its package.json.
  • Running files, watch mode, the REPL, and tests.
  • Using the finished reference project.
  • Troubleshooting common errors.

1. Install Node

Install Node 26 LTS (or 24 LTS) with a version manager โ€” nvm, fnm or Volta (Chapter 1) โ€” or the installer from nodejs.org. Then check:

Any editor works; VS Code has excellent Node support (debugging, a JavaScript Debug Terminal, test integration).

2. The playground folder

Clone the repository (or use Code โ†’ Download ZIP on the repository page):

The final playground argument names the folder playground/, so every path in this book (playground/hello.js, playground/reference/) matches your copy exactly. Inside it:

"type": "module" makes every .js file an ES module, so import/export and top-level await work everywhere, as in the book. "private": true prevents accidental publishing.

3. Running code

4. Experimenting in the REPL

Type node to open the REPL. Core modules are available without imports, and top-level await works:

5. Running tests

Name test files something.test.js (or put them in a test/ folder) and run npm test (Chapter 14):

6. The reference project

playground/reference/ is the complete Chapter 16 notes API with its tests. Use it to compare your solution, or to see a feature working before you build it:

7. Troubleshooting

ErrorCauseFix
No playground/ folder on your machineyou are reading the book onlinegit clone https://github.com/coder-hop/nodejs-book-playground.git playground
SyntaxError: Cannot use import statement outside a modulethe file is treated as CommonJSrun it inside playground/ (which has "type": "module") or rename to .mjs
ERR_MODULE_NOT_FOUNDmissing file extension in a relative import, or wrong pathwrite "./lib/util.js", not "./lib/util" (Chapter 3)
ReferenceError: __dirname is not definedCommonJS variable in an ES moduleuse import.meta.dirname
ENOENT: no such file or directorypath relative to the wrong working directoryrun from the right folder, or build paths from import.meta.dirname (Chapter 9)
EADDRINUSE: address already in useanother server (often your previous run) holds the portstop it (Ctrl+C) or use another PORT
The program never exitsan open server, interval or stdin listenerclose it, clearInterval, or .unref() (Chapter 7)
The program exits too early (code 13)top-level await on something nothing will ever settlecheck that a timer/server keeps the loop alive; AbortSignal.timeout alone doesn't (Chapter 8)
ERR_UNSUPPORTED_TYPESCRIPT_SYNTAXenum or parameter property in a .ts fileuse erasable syntax (Appendix A)

Ready?

Start with Chapter 1: Introduction to Node.js. Run every example yourself, change it, break it, and read the error messages โ€” they're usually precise.