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
node --watch hello.jsre-runs the file every time you save it (or anything it imports). Stop with Ctrl+C.node --env-file=.env app.jsloads environment variables from a file (Chapter 2).node -p "expression"prints the value of a one-liner.- Servers keep running until you press Ctrl+C; the book's examples close themselves so they can be checked automatically.
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
| Error | Cause | Fix |
|---|---|---|
No playground/ folder on your machine | you are reading the book online | git clone https://github.com/coder-hop/nodejs-book-playground.git playground |
SyntaxError: Cannot use import statement outside a module | the file is treated as CommonJS | run it inside playground/ (which has "type": "module") or rename to .mjs |
ERR_MODULE_NOT_FOUND | missing file extension in a relative import, or wrong path | write "./lib/util.js", not "./lib/util" (Chapter 3) |
ReferenceError: __dirname is not defined | CommonJS variable in an ES module | use import.meta.dirname |
ENOENT: no such file or directory | path relative to the wrong working directory | run from the right folder, or build paths from import.meta.dirname (Chapter 9) |
EADDRINUSE: address already in use | another server (often your previous run) holds the port | stop it (Ctrl+C) or use another PORT |
| The program never exits | an open server, interval or stdin listener | close it, clearInterval, or .unref() (Chapter 7) |
| The program exits too early (code 13) | top-level await on something nothing will ever settle | check that a timer/server keeps the loop alive; AbortSignal.timeout alone doesn't (Chapter 8) |
ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX | enum or parameter property in a .ts file | use 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.