Sub topic 10
Creating the app with create vite
This chapter covers scaffolding a React project with create vite. It assumes you know JavaScript modules, can use a terminal, and have Node.js and npm installed (the previous chapter covered checking them). It also assumes you know what a component is and why React projects need a build tool.
What the command does
Scaffolding means generating a starting set of files from a template. Vite's scaffolding tool is a separate package called create-vite. You never install it yourself. The npm create command downloads it, runs it once, and discards it.
npm create vite@latest my-app -- --template react
cd my-app
npm install
npm run devEach part of the first line has a job:
| Part | Meaning |
|---|---|
| `npm create` | Shorthand for `npm init`. It runs the package named `create-<name>`. |
| `vite@latest` | Runs `create-vite` at its newest published version, so the templates are current. |
| `my-app` | The project name. It becomes the folder name and the `name` field in `package.json`. |
| `--` | Ends npm's own options. Everything after it goes to `create-vite`. |
| `--template react` | Picks the plain JavaScript React template. |
The -- matters. In current npm versions, flags placed after the package name are passed on to the tool only when this separator is present. Without it, npm may treat --template as its own flag and ignore it. You would then see the interactive prompts instead of getting the template you asked for.
Interactive and non-interactive use
If you run npm create vite@latest with no arguments, create-vite asks questions. It asks for a project name, a framework, and a variant. The React choices include JavaScript, TypeScript, and variants that use the SWC compiler. The exact prompts change between versions of create-vite, so read what is on screen instead of relying on memory.
Passing --template skips the framework question. That makes the command repeatable, which helps in documentation, scripts, and workshops. Other template names you may see are react-ts (TypeScript) and react-swc (a faster compiler for JSX).
You can also scaffold into the current folder by using . as the name:
mkdir my-app
cd my-app
npm create vite@latest . -- --template reactIf the folder is not empty, create-vite asks whether to cancel, remove the existing files, or ignore them. Read that prompt carefully. Choosing to remove files deletes them.
Why there are separate install and run steps
The scaffold only writes files. It does not install anything the project depends on. After the scaffold, the project has a package.json that lists dependencies, but no node_modules folder.
npm install does that second part. It reads package.json, downloads each package, and places them in node_modules. It also writes package-lock.json, which pins the exact versions it installed. Commit the lock file so that other people and your build servers get the same versions.
npm run dev runs the script named dev from the scripts section of package.json. For the react template, that script is just vite. The command starts the dev server and prints something like this:
VITE v7.x.x ready in 300 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to exposeThe version number and timing will differ on your machine. Open the Local address in a browser. You should see the template's starter page, with the Vite and React logos and a button that counts clicks.
Checking that it worked
Run these checks when something looks wrong:
- List the folder. You should see
index.html,package.json,vite.config.js, and asrcfolder. - Confirm that
node_modulesexists afternpm install. - Read the terminal output of
npm run devfor the actual address. If the default port is already in use, Vite picks the next free one, such as 5174, and prints it. - Open the browser's developer console. Errors from your code appear there, not in the terminal.
Common errors
npm: command not foundornode: command not found. Node.js is not installed or not on yourPATH. Revisit the previous chapter.- A Node version error. Recent Vite versions require a minimum Node.js version. The error message names it. Upgrade Node, or use a version manager such as nvm to switch.
- Prompts appear even though you passed
--template. The--separator is missing or in the wrong place. It goes after the project name and before the flags. vite: command not foundwhen runningnpm run dev. You skippednpm install, so Vite is not innode_modules. Run it and try again.- The page does not load at
localhost:5173. Check the terminal for a different port. If you run the server in a container or on a remote machine, start it withnpm run dev -- --hostso it listens on the network instead of only on the loopback interface. - A project name with spaces or capital letters. The name becomes a
package.jsonname, which npm restricts. Use lowercase letters and hyphens.
Choosing a template variant
The variants differ in language and compiler, not in how React works. The following table helps you choose.
| Template | Language | Notes |
|---|---|---|
| `react` | JavaScript | Used throughout this tutorial. Files end in `.jsx`. |
| `react-ts` | TypeScript | Adds type checking. Files end in `.tsx`. |
| `react-swc` | JavaScript | Swaps the Babel-based plugin for one built on SWC. |
| `react-swc-ts` | TypeScript | Combines the two above. |
Everything else in this tutorial uses react. The component, props, and state code is identical in the TypeScript variants apart from type annotations.
Stopping and restarting
The dev server runs until you stop it. Press Ctrl+C in the terminal to stop it. Run npm run dev again to restart it. If you want the browser to open automatically, use npm run dev -- --open.
You only run npm install again when dependencies change, for example after pulling someone else's changes to package.json or after adding a package.
Exercise
You run npm create vite@latest shop --template react and Vite asks you to choose a framework anyway. What went wrong, and what is the corrected command?
Show answer
The -- separator is missing, so npm consumed --template react as its own option instead of passing it to create-vite. The corrected command is npm create vite@latest shop -- --template react.
Exercise
After scaffolding, you run npm run dev right away and get vite: command not found. Why does this happen, and what do you run first?
Show answer
The scaffold writes files but does not install dependencies, so Vite is not in node_modules yet. Run npm install inside the project folder, then run npm run dev again.
Exercise
Your teammate clones the project from Git and wants the same dependency versions you have. Which file makes that possible, and which command uses it?
Show answer
package-lock.json pins the exact installed versions, and it must be committed. Running npm install (or npm ci for a clean, exact install) reads it.