You’re on the vite branch, and it still points at the same commit as main. The README’s first to-do, the “Open now” badge, needs a date library, which you’ll get from npm. Before you install anything, though, the site needs to become an npm project, with one file that describes it and holds its settings.
In this lesson, you’ll turn the store’s site into an npm project. You’ll create its package.json, fix the one default that doesn’t fit the store’s code, and run the check script with a shorter command. Then you’ll check that main still has the site as it was and commit the new file on vite. Start by asking npm to set up the project.
Create the Project File
The badge’s date library will come from the npm registry, a public collection of JavaScript packages that anyone can download. A package manager downloads packages from there and keeps track of which ones your project uses. npm is the package manager that comes with Node, so it arrived on your computer when you installed Node. Some projects use pnpm instead. It does the same job with its own commands, but this course sticks with npm.
npm keeps a project’s details in a file called package.json in the project’s top folder. The commands for installing the date library and building the site read that file. npm would write a bare one the first time you install a package. If you create it yourself first, the project gets its name and description, and you get a place to set how Node runs the store’s code and to add scripts before any package arrives.
Run npm -v. If it shows a version below 12, update npm once with npm install -g npm@latest.
Then make sure you’re on vite. The first line of git status should say On branch vite, and if it says main, run git switch vite. Now create the file with npm init -y. The npm init command would normally ask you a series of questions about the project, and the -y option, short for yes, accepts a default answer for each one:
$ npm init -y
Wrote to /Users/martin/projects/generic-grocery/package.json:
{
"name": "generic-grocery",
"version": "1.0.0",
"description": "The website of Generic Grocery Co., a family-owned ...",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"type": "commonjs"
}
npm wrote a new package.json and printed what’s in it. The file is JSON, and npm filled in the fields with defaults and details from the folder:
"name"comes from the folder’s name,generic-grocery."description"is the first paragraph of the README. It’s cut short with...here and in the rest of this lesson, while your file keeps the whole paragraph."main"names the file that other projects would load if you published this one as a package. The site isn’t a package, so you can leave it alone."scripts"holds commands for the project. npm adds a placeholder called"test"that prints an error and signals failure, because it doesn’t know of any tests."type"tells Node how to run the project’s.jsfiles, and it says"commonjs".
Your path in the first line of the output shows your own folder. After a change to the project, run the check script to see where things stand. This time, node scripts/check.js stops with an error:
(node:12182) Warning: Failed to load the ES module:
/Users/martin/projects/generic-grocery/scripts/check.js. Make sure to set
"type": "module" in the nearest package.json file or use the .mjs extension.
...
SyntaxError: Cannot use import statement outside a module
...
The script that worked a minute ago doesn’t run anymore, and the only thing that changed is the new package.json. The number after node: is different on every run, and Node prints the warning on one line, which is wrapped here for display. The warning above the error already points at the cause, so the next step is to understand what "type" does.
Make the Project Use ES Modules
The store’s code is written as ES modules, short for ECMAScript modules. Every file uses import and export, and so does scripts/check.js.
Node also supports an older module system, CommonJS, which loads code with require() instead. Node needs to know which of the two a .js file uses.
Until now, the project had no package.json, so Node looked inside check.js, found import, and ran it as an ES module. Now package.json says "type": "commonjs", and Node follows that setting instead of guessing. It runs the script as CommonJS, which doesn’t understand import.
The pages in the integrated browser still work, because browsers don’t read package.json. The type="module" in the <script> tags of the home and deals pages tells them that those scripts are ES modules.
You can solve it by telling Node the truth about the store’s code. Open package.json in VS Code and make two changes. Set "type" to "module", and replace the "test" placeholder with a "check" script:
package.json
{
"name": "generic-grocery",
"version": "1.0.0",
"description": "The website of Generic Grocery Co., a family-owned ...",
"main": "index.js",
"scripts": {
"check": "node scripts/check.js"
},
"keywords": [],
"type": "module"
}
Watch the commas when you edit JSON. The "check" line is the only entry in "scripts", so it has no comma after it, while "keywords": [] still needs its comma. Save the file. An npm script is a named command in "scripts", and npm run followed by the name runs it from the project’s top folder:
$ npm run check
npm notice run [email protected] check
npm notice run node scripts/check.js
Checking the Git repository in /Users/martin/projects/generic-grocery
✔ Git is installed (git version 2.43.0).
✔ The project folder is a Git repository.
✔ Your commits are labeled Martin Breuss <[email protected]>.
✔ .gitignore keeps notes.txt out of the repository.
✔ .gitignore keeps .DS_Store out of the repository.
✔ The history has 11 commit(s).
✔ npm is version 12 or later.
✔ You're on the vite branch.
✔ package.json has "type": "module".
✔ package.json has a "check" script, so npm run check runs this report.
✔ .gitignore keeps node_modules/ out of the repository.
✔ No tracked file holds a Generic Curbside secret key (curb_sec_...).
! Not everything is committed yet: package.json.
Next step: package.json is new to Git (git status lists it under
"Untracked files"): commit it, or delete it if you don't want it.
All checks passed, with 1 thing(s) worth a look.
The check runs again! The two npm notice lines at the top show which script npm runs and what that script’s command is. The report has grown, too, and its next step is one line in your terminal, wrapped here for display. The check script notices package.json now, so it checks the "type" setting and the "check" script you added. It also confirms that .gitignore already ignores node_modules/, the folder where npm will put installed packages.
From here on, npm run check is how you run the report on vite. A script name is shorter to type than a path, and you can use the same command in every project that defines that script, which is why teams put their everyday commands in "scripts".
If it still seems odd that a file the browser never reads decides whether the store’s code runs, then ask the mentor about it. The button below opens a chat with the question filled in. Send it as it is, or add what puzzles you, and the mentor can explain how Node and the browser each find out that a file is an ES module:
Whatever example the mentor gave, Node reads "type" from package.json and the browser reads type="module" from the <script> tag, so one setting doesn’t replace the other. With the check running again, package.json is the one thing left to deal with.
Check the Site on main
The warning near the end of the report says that package.json isn’t committed yet. Before you commit it, you may want to make sure that main still has the site exactly as it was, because keeping main safe is the reason you made a branch. Switch over:
$ git switch main
Switched to branch 'main'
The last lesson showed that switching puts the files of that branch’s latest commit in your folder. So is package.json there on main? Make a guess, then look at the Explorer in VS Code and run git status:
package.json is still in the Explorer, and Git lists it on main:
$ git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
package.json
nothing added to commit but untracked files present (use "git add" to track)
If you expected it to stay behind on vite, then you’re in good company, because almost everyone expects that the first time. A branch points at a commit, and package.json isn’t in any commit yet. It’s only in your working tree, the folder you see and edit, and you have one working tree for all your branches.
So uncommitted work doesn’t belong to a branch yet. It becomes part of a branch when you commit it there.
When you switch, Git swaps the files that differ between the two branches’ commits. It leaves untracked files and uncommitted edits where they are, so they come along to the branch you switch to.
If a switch would overwrite one of them, Git refuses instead and lists the files. For an edited file, the message starts with “error: Your local changes to the following files would be overwritten by checkout”. For an untracked file like package.json, it starts with “error: The following untracked working tree files would be overwritten by checkout”.
If you committed package.json on main, the move would land on the branch you’re keeping safe. Go back to vite, stage the file by name, and commit it there:
$ git switch vite
Switched to branch 'vite'
$ git add package.json
$ git commit -m "Turn the site into an npm project" -m "Use ES modules."
[vite 889332f] Turn the site into an npm project
1 file changed, 11 insertions(+)
create mode 100644 package.json
The [vite 889332f] at the start of the output tells you which branch got the commit. Your hash will be different, because it depends on your name and the time of your commit. Now repeat the earlier trip to main to see the result you expected:
$ git switch main
Switched to branch 'main'
$ git status
On branch main
nothing to commit, working tree clean
And… package.json is gone from the Explorer, and main is back to the site as the store uses it. Run git switch vite, and the file is back. This time, Git put it there, because it’s in the commit that vite points at.
On main, keep using node scripts/check.js for the report until the move is merged, because main has no package.json for npm run check to read. On vite, run npm run check once more, and its last line says All checks passed.
To test whether the surprise on main makes sense now, explain it in your own words. The button below opens a chat with the mentor and fills in a first line. Add the git status output that listed package.json as untracked on main and a sentence or two on why package.json was there and how you kept it off main, then send it. The mentor responds to your explanation and asks about any part that doesn’t hold up:
Whether the mentor confirmed your explanation or pointed out a gap, keep committing before you switch for the rest of the move, so each branch keeps its own work.
Check Your Setup
To see whether you can set up an npm project and run its scripts, answer these questions before you open the answers:
- You run
npm init -yin your own recipe site, whose files useimport. Afterward,node build.jsfails with “Cannot use import statement outside a module”. What do you change? - You add
"prices": "node scripts/prices.js"to the"scripts"of yourpackage.json. Which command runs it?
- Set
"type"to"module"inpackage.json.npm init -ywrites"commonjs", and Node then runs every.jsfile as CommonJS, which doesn’t understandimport. - Run
npm run prices.npm runfollowed by a script’s name runs its command.
The store’s site is an npm project now, on vite only, and main still has the site that works today. The badge needs date math that JavaScript’s built-in Date makes clumsy, so it’s time to bring in the date library. In the next lesson, you’ll check that library, then install it with npm.