Sub topic 10
Creating the app with create vite
This chapter assumes you have Node.js and npm installed and can run commands in a terminal. It also assumes you know what a component is. Here you will generate a working React project and start it.
What `create vite` does
create vite is a scaffolding tool. Scaffolding means generating a starter set of files so you don't write the project layout by hand. The npm create command runs the create-vite package from the npm registry. That package asks for a few choices, then copies a template into a new folder.
Nothing is installed globally. The @latest tag makes npm fetch the newest version of the tool each time.
Run the commands
npm create vite@latest my-app -- --template react
cd my-app
npm install
npm run devEach line does one job:
npm create vite@latest my-app -- --template reactcreates themy-appfolder from the React template, which uses JavaScript and JSX.cd my-appmoves into the new project.npm installreadspackage.jsonand downloads React, Vite and the other dependencies intonode_modules.npm run devstarts the development server.
The terminal then prints a local address, by default http://localhost:5173. Open it in a browser to see the starter page.
Interactive prompts
If you leave out the project name or the template, create-vite asks you questions. You choose a framework (React), then a variant, such as JavaScript or TypeScript. Newer versions may also ask whether to install dependencies and start the server right away. Passing the arguments, as above, skips the questions and gives the same result every time.
If the target folder already exists and is not empty, the tool asks whether to cancel, remove the existing files or ignore them. Read that prompt carefully before you answer.
The flow at a glance
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error about an unsupported Node version | Node.js is older than the version Vite requires | Check `node --version` against the Vite docs, then upgrade |
| `vite: command not found` | You skipped `npm install` | Run `npm install` inside the project folder |
| `npm run dev` fails with a missing `package.json` | You are in the wrong folder | Run `cd my-app` first |
| Server runs on 5174 instead of 5173 | Port 5173 is already in use | Use the address the terminal prints, or stop the other process |
Stop the dev server at any time with Ctrl+C in the terminal where it runs.
What you have now
You have a runnable React project, but you haven't looked at its files yet. The next chapter walks through them one by one, starting with index.html and package.json.
Exercise
You run npm create vite@latest my-app --template react on a recent npm version, and the tool asks you to choose a template anyway. What went wrong, and how do you fix the command?
Show answer
npm consumed --template react as its own flag because the arguments were not separated. Add a standalone -- before the flags: npm create vite@latest my-app -- --template react.