Start From Something That Works

Grace’s discount question can wait for now. Today is your first hour with the code behind the shop. It’s tempting to open the source files straight away, but the shop’s own README asks you to get it running and use one feature as staff would before you decide what to read.

In this lesson, you’ll start the shop, place a real order, and write down one question and the few files where you might find the answer. Start with its README, as you would with any new project.

Run the Shop From Its README

On first contact with a project, if you read its source before you’ve seen it run, you build a picture from filenames and guesses that one run could correct. So for this first hour, you run first. In later lessons, you read first and run the code to check your understanding.

The “First visit” section of the shop’s README.md asks for Node.js 22 or later and Git, with no packages to install with npm, Node.js’s package manager. In a terminal, node --version and git --version should each print a version number. If you need to update Node.js, Latest Node Version explains which release to install.

Next, run the README’s four commands in order, plus one git checkout line after cd. Select your operating system:

Language: Windows PowerShell
PS> git clone https://github.com/realjs/second-draft-books.git
PS> cd second-draft-books
PS> git checkout 3d15f97
PS> npm run seed
PS> npm start
Language: Shell
$ git clone https://github.com/realjs/second-draft-books.git
$ cd second-draft-books
$ git checkout 3d15f97
$ npm run seed
$ npm start

The first two commands download the project and move you into its folder. git checkout 3d15f97 pins your copy to the snapshot this course was written against. Git’s note about a “detached HEAD” only means you’re on a fixed commit instead of a branch.

npm run seed loads the shop’s seed data, a starting set of fictional books, customers, and orders, and prints one line:

Language: Text
Restored fictional shop records. No email was sent and no card was charged.

Then npm start starts the shop’s server and tells you where to find it:

Language: Text
> [email protected] start
> node server.js

The Second Draft: http://127.0.0.1:3000/admin/

The terminal now looks stuck. That’s the server waiting for requests until you press Ctrl+C. Leave it running, and open a second terminal in the same second-draft-books folder for any other commands.

Place One Order and See What Changed

A running server only shows that the shop starts. To see what it does, use one feature the way staff would. Open http://127.0.0.1:3000/admin/ in your browser. This is the staff desk, where staff place orders, record returns, and adjust shelf counts.

Before you change anything, scroll down the staff desk and note three things: Orders has one row, order-1. Shelf count shows book-1: 18 on hand, 0 reserved, and book-1 is Small Maps. Outbox is empty. Now place the order the README describes, the one this course follows to the end:

  1. Under Customer, choose Nadia Patel. The seed data stores her as customer-1.
  2. Under Book, choose Small Maps · £7.50.
  3. Leave Copies at 1.
  4. Type READ20 into Discount code.
  5. Click Place order.

A line under the form says Saved order-2. Scroll down and compare what you see with your notes:

  • Orders has a new row for order-2, paid £6.00, with the status paid. That’s £7.50 with 20 percent off, stored as 600p because the shop keeps money in whole pence.
  • Shelf count shows book-1: 17 on hand, 0 reserved, down from the 18 you noted.
  • Outbox lists Your order order-2 — Thanks for visiting The Second Draft. Total paid: £6.00..

The shop also wrote that confirmation to outbox/email-1.json instead of sending an email. Write the three changes down. You saw the before and the after yourself, so they’re evidence about what the code does, gathered without reading a line of it. To start over, stop the server, rerun npm run seed, and start it again.

Buttons like the one below open Code Mentor, RealJS’s AI mentor, in a chat drawer beside the lesson, with a question typed for you to edit or send. For now, use it to talk through your approach, such as why you ran the shop first:

Find the Commands in package.json

The README told you to type npm start, but where does npm get start from? A Node.js project’s top folder has a package.json file, whose scripts section lists the commands npm knows:

Language: JSON
"scripts": {
    "start": "node server.js",
    "test": "node --test",
    "seed": "node scripts/seed.js",
    "weekly-report": "node scripts/report.js"
}

The "start" line says that npm start runs node server.js, which makes server.js the shop’s entry point: the file where the program starts. You don’t need to open it today.

Now compare the README’s “Useful commands” section with scripts. The README lists npm run report, but the scripts section has no "report" command, only "weekly-report". Don’t let the scripts/ folder confuse you: it does hold a file called report.js, and "weekly-report" is the command that runs it.

Someone renamed the npm command, not the file, and the README didn’t follow, so npm run report fails while npm run weekly-report works. When notes and files disagree, the files determine what runs. A stale line like this is worth noting and mentioning to the team, and it goes in your map shortly.

Choose One Question to Answer

Grace keeps questions for new starters in docs/FIRST-HOUR.md. One asks, “Where does the confirmation come from?” The confirmation is the email that appeared in the outbox. Before you read on, write your plan in a sentence or two: Which files would you open, and how much of the code do you need to understand to answer her? You’ll compare it with what the answer takes.

You can narrow the search without opening any code. Here’s an overview of the project’s folders:

  • docs/: The team’s notes, including Grace’s questions
  • public/admin/: The staff desk you used a moment ago
  • src/: The shop’s code, in 11 folders named after what the shop does, such as orders/, stock/, and email/
  • scripts/: The seed and report commands
  • data/: The shop’s records, as JSON files that npm run seed resets
  • outbox/: The confirmations the shop writes instead of sending email
  • test/: Automated tests, which you can leave unopened for now

The confirmation appeared when you placed an order, so src/orders/ and src/email/ are the places to look. List their files in your second terminal with ls src/orders and then ls src/email, or open both folders in your editor’s file sidebar. Of those files, src/orders/create.js and src/email/templates.js have the names that fit best.

Grace’s notes agree. Under this question, docs/FIRST-HOUR.md says to start at createOrder() in src/orders/create.js and follow the email call only. Like the README, notes are a claim until you’ve checked them.

Check it now. Open src/orders/create.js and find the one line in createOrder() that mentions email, two lines above return order;:

Language: JavaScript
queueEmail(state, customer, orderMessage(order));

orderMessage(order) builds a message from your order, and queueEmail() puts it in the outbox queue. The notes held up. The confirmation starts here, in createOrder(). Where orderMessage() gets its wording is the open thread, and the next lessons follow it into src/email/.

Compare that with your plan. By reading one line of one file, you found where the confirmation starts. If you planned to read from server.js until you understood the shop, you’re in good company. Many people believe they must understand every line of a codebase before doing anything safely in it.

But src/ holds 32 JavaScript files, and one line of one of them told you where to start. What makes an answer safe is checking it against the code and a run, not having read everything first.

If you still feel lost, that’s the normal state of anyone in their first hour on a project, experienced developers included. The way out is to answer one small question, as you did here, not to wait until you feel ready. Three rules carry you through this course:

  1. Remember that you’ll never need to read all of it. Nobody on a real team has.
  2. Start from something you can see working.
  3. Follow one thread at a time, and write down what you find.

To follow the third rule, you need somewhere to write. Create map.md outside the second-draft-books folder, so it stays yours whatever happens in the repository. This is your map of the shop, and it grows throughout the course. Its first entry holds what you’ve found:

Language: Markdown Text
## Where does the confirmation come from?

- Start command: npm start, which runs node server.js
- Seen: order-2 paid £6.00, and outbox/email-1.json
  says "Thanks for visiting The Second Draft.
  Total paid: £6.00."
- Files that look relevant: src/orders/create.js,
  src/email/templates.js
- Noted: README says npm run report, but
  package.json has weekly-report. Tell the team.

The heading is your one question, specific enough that you’ll know when it’s answered. Your wording can differ, as long as each line is something you saw or a file you can name.

To sharpen your question, press the button below, paste your map entry under the question, and send it. The mentor will help you narrow the question rather than explain the shop’s code:

Check Your Understanding

Answer these from your notes and your run before you open the answers:

  1. Which command starts the shop, and which file does that command run?
  2. The README lists npm run report. Where do you check whether that command exists, and what do you find?
  3. What did your Small Maps order with READ20 cost, and what else changed in the shop when you placed it?
  4. A teammate plans to read every file in src/ before answering Grace’s confirmation question. What would you suggest instead?
  1. npm start starts the shop. The "start" line in the scripts section of package.json runs node server.js, so server.js is the entry point.
  2. Check the scripts section of package.json, not the scripts/ folder. It has a "weekly-report" command, which runs scripts/report.js, but no "report" command, so that README line is stale and goes in your map.
  3. The order cost 600p, shown as £6.00. Small Maps went from 18 on hand to 17, and a confirmation for order-2 appeared in the outbox and outbox/email-1.json.
  4. Write the question down, skim the folders, and start from the ones that fit: src/orders/ and src/email/. One line of createOrder() shows where the confirmation starts. Checking an answer against the code and a run makes it safe, not reading everything.

Next comes Exercise: Name Your Starting Points, where you’ll record the start command, the entry point, your order’s total, and the files you’d open first for Grace’s confirmation question. It’s the course’s first exercise. You type answers into an editor in the browser, run them, and automated tests tell you which hold up. Run early and often, read each failing test’s message for a clue, and use the Full screen button for more room.

Become a Member to join the conversation.