Exercise: Book Club Picks

Split the Chapter Two Book Club's one long planner file into three modules with named exports, add npm scripts, install date-fns at a pinned version to format the meeting date, and keep node_modules out of Git, with node --test checking every step.

Apprentice · Node.js · JavaScript · Updated

By

Chapter Two Book Club plans its meetings with one long file, club.js: the club's picks, the members' votes, the counting, and a hand-made way to write a date. It works, but every change means scrolling past everything else, and the date code is the kind of thing a package already does. You'll set it up the way real Node projects are: three modules that each do one job, npm scripts, one package installed at a pinned version, and a .gitignore. Tests check every module and every setting as you go.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-node-book-club-picks, also linked from the Exercise box beside the lesson. Clone it into your nodejs-course folder. In VS Code's terminal:

cd ~/nodejs-course
git clone https://github.com/ask-codey/exercise-node-book-club-picks.git

Git makes a folder, exercise-node-book-club-picks, with the exercise code inside. Open that folder in VS Code (File > Open Folder) and open its terminal with Terminal > New Terminal. There's nothing to install yet: installing the one package is a step of the exercise. Get the Exercise Code with Git walks through cloning step by step, if you'd like a reminder.

The folder holds:

  • club.js: the whole planner in one file. It works: node club.js prints the next pick and meeting.
  • package.json: the project's settings, as npm init made them, with "type": "module".
  • club.test.js: the tests, which import your modules, read package.json and .gitignore, and run main.js. You don't need to change it.

What to do

Run the planner first, so you know what it should still print at the end:

node club.js
Chapter Two Book Club
Next pick: A Map of Quiet Rivers (3 votes)
Read 89 pages a week for 4 weeks.
Next meeting: Thursday, December 3

The comment at the top of club.js lists where everything goes:

books.js   export books (the four picks below), findBook(id) and pagesPerWeek(book, weeks)
             findBook(3)                   the book with id 3: { id: 3, title: "A Map of Quiet Rivers", pages: 356 }
             findBook(9)                   undefined: the club has no book 9
             pagesPerWeek(findBook(3), 4)  89: 356 pages over 4 weeks is 89 a week, rounded up
votes.js   export votes (the seven votes below), countVotes(votes) and winner(votes)
             countVotes(votes)             { 1: 2, 2: 1, 3: 3, 4: 1 }: each book's id and how many votes it got
             winner(votes)                 3: the book id with the most votes. In a tie, the smaller id wins,
                                           so for one vote for book 2 and one for book 1, it's 1.
main.js    imports what it needs from books.js and votes.js, and prints the same four lines as this file.
           It formats the meeting date with the format function from the date-fns package, using the pattern
           "EEEE, MMMM d", instead of the DAYS and MONTHS lists below.

The functions in club.js already work: you move them, you don't rewrite them. Then:

  1. Make books.js and votes.js. Move the data and the functions into them, each with export in front.
  2. Make main.js. Move the last lines of club.js into it, and import what they use from ./books.js and ./votes.js.
  3. Install date-fns at exactly 4.4.0, as a dependency: npm install date-fns@4.4.0 --save-exact. In main.js, import format from "date-fns", and replace formatDate(nextMeeting) with format(nextMeeting, "EEEE, MMMM d"). The DAYS and MONTHS lists and formatDate aren't needed anymore.
  4. Set the scripts in package.json: "start" runs "node main.js", and "test" runs "node --test".
  5. Add a .gitignore file with one line, node_modules/, so Git leaves the installed package out.
  6. Delete club.js.

Check your work

Until package.json has its test script, run the tests with Node itself:

node --test

Once the test script is node --test, npm test runs the same thing (until then it only says no test specified). At first all 15 tests fail. That's expected: each step turns its tests green. A failing test's message says what's missing or what came back, for example books.js doesn't exist yet or The start script should be "node main.js", but it's undefined.

With the start script in place, npm start runs main.js.

Hints

  • One module at a time. Make books.js first and run node --test: tests 1 to 4 turn green. Then votes.js, then main.js.
  • A function needs what it uses. winner calls countVotes, so they live in the same file. findBook uses books, so they do too.
  • Named exports, imported by name. Modules: import and export shows both sides: export in front of each function, and import { findBook, pagesPerWeek } from "./books.js"; with the ./ and the .js.
  • A package is imported by its name alone. import { format } from "date-fns"; has no ./ and no .js, as npm and Packages shows.
  • Exactly 4.4.0, with no caret. Package Versions, npm ci and npm audit shows what --save-exact writes. If package.json says "^4.4.0", install it again with --save-exact.
  • A number comes out wrong? Set a breakpoint in main.js and step into the function, as Debugging with Breakpoints shows.

When you're done

Every test is green, and npm start prints what club.js printed, now from three modules and a package:

> book-club-picks@1.0.0 start
> node main.js

Chapter Two Book Club
Next pick: A Map of Quiet Rivers (3 votes)
Read 89 pages a week for 4 weeks.
Next meeting: Thursday, December 3

Your folder now holds books.js, votes.js, main.js, .gitignore, package.json, package-lock.json and node_modules. Now look at the club's next job: the members want to see how many pages each book has before they vote. Which file would you change, and which files would you leave alone?

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.

  1. On GitHub, click +, then New repository. Name it (for example exercise-node-book-club-picks), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-node-book-club-picks.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-node-book-club-picks, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

Modules that each do one job, scripts anyone can run, a package pinned to the version you tested, and a .gitignore that keeps node_modules out: that's how every Node project you'll join is set up, and now you've set one up yourself.

Key terms

Related