Sub topic 11
Tour of the generated files
This chapter walks through every file that npm create vite@latest my-app -- --template react produces. It assumes you can read JavaScript modules and have run the scaffold command from the previous chapter. You should also know what a component and a prop are.
The exact file list changes slightly between Vite versions. The roles of the files stay the same, so focus on the roles.
The layout at a glance
After scaffolding and npm install, the project looks like this:
my-app/
├── node_modules/
├── public/
│ └── vite.svg
├── src/
│ ├── assets/
│ │ └── react.svg
│ ├── App.css
│ ├── App.jsx
│ ├── index.css
│ └── main.jsx
├── .gitignore
├── eslint.config.js
├── index.html
├── package.json
├── package-lock.json
├── README.md
└── vite.config.jsindex.html: the real entry point
In many older build setups, index.html is a template hidden inside a config folder. In Vite it sits at the project root, and it is the entry point of the app. Vite reads it, finds the script tag, and follows the imports from there.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>Two lines matter most:
<div id="root"></div>is an empty container. React fills it at run time.<script type="module" src="/src/main.jsx">loads your code as a native ES module. The browser then follows eachimportstatement.
Edit the <title> and the icon here. Nothing in React changes the page title by default.
src/main.jsx: connecting React to the page
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>,
)createRoot takes a DOM element and returns a root object. Calling render on it hands React the element tree to display. This file runs once, at startup.
StrictMode is a development helper. It renders components an extra time and re-runs effects to expose impure code. It has no effect in a production build and adds no DOM elements.
Note the explicit .jsx extension in './App.jsx'. Vite resolves it either way, but the extension is the convention in the template.
src/App.jsx: the root component
App is the top of your component tree. The template version uses useState and logos. You can replace it with something small to see the structure:
import Counter from './Counter.jsx'
function App() {
return (
<main>
<h1>My first React app</h1>
<Counter label="Clicks" />
</main>
)
}
export default AppThe export default line is what lets main.jsx write import App from './App.jsx'. If you forget it, the import fails with a message that App is not exported.
The CSS files and assets
src/index.cssholds global styles. It is imported once frommain.jsx.src/App.cssholds styles forApp. CSS imports are not scoped, so these rules apply to the whole page.src/assets/holds files you import from JavaScript, such asimport logo from './assets/react.svg'. Vite fingerprints these files in production builds, so their names include a content hash for caching.
public/: files served as they are
Files in public/ are not processed. They are copied unchanged to the root of the build output and referenced with absolute paths like /vite.svg.
Use the two folders for different jobs:
| Folder | Referenced by | Processed by Vite | Typical content |
|---|---|---|---|
| `src/assets/` | `import` in JavaScript or CSS | Yes, hashed and optimized | Images used by components |
| `public/` | Absolute URL such as `/robots.txt` | No, copied as is | Favicon, `robots.txt`, files needing a fixed name |
A common error is putting an image in public/ and importing it with a relative path. Reference it with /name.png instead, or move it into src/assets/.
package.json: scripts and dependencies
{
"name": "my-app",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"react": "^19.0.0",
"react-dom": "^19.0.0"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.3.0",
"eslint": "^9.0.0",
"vite": "^6.0.0"
}
}Version numbers here are examples. Your file will show newer ones.
"type": "module"makes Node treat.jsfiles as ES modules. This is whyvite.config.jscan useimportandexport.scriptsdefines whatnpm run dev,npm run buildandnpm run previewdo.dependenciesship with the app.devDependenciesare only used while building. Vite itself is a dev dependency because the browser never loads it."private": truestops you from publishing the project to npm by accident.
package-lock.json pins the exact version of every installed package. Commit it so teammates and CI install the same tree.
vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
})The React plugin transforms JSX and enables fast refresh. A later chapter covers more options.
Remaining files
eslint.config.jsconfigures the linter, including React hooks rules. Run it withnpm run lint..gitignoreexcludesnode_modulesanddist. Never commitnode_modules.README.mdis a template note. Replace it with your own.node_modules/holds installed packages. It is generated bynpm installand can be deleted and recreated at any time.
Common mistakes
- Editing files inside
node_modulesordist. Changes are lost on the next install or build. - Naming a file with JSX as
.js. Vite's default setup expects.jsxfor files containing JSX, and a.jsfile raises a parse error. - Starting a component name with a lowercase letter. React treats
<counter />as an HTML tag, not your component. - Moving
index.htmlintosrc/. Vite looks for it at the project root.
Exercises
Exercise
You put photo.png in public/ and want to show it in a component. What src value do you use, and why?
Show answer
Use /photo.png. Files in public/ are served unprocessed from the site root, so you reference them by absolute URL. They are not imported through the module system.
Exercise
Name the file that tells the browser which JavaScript to load first, and the line in it that does so.
Show answer
index.html, with the line <script type="module" src="/src/main.jsx"></script>.
Exercise
After npm run build, why does my-app/node_modules not need to be deployed?
Show answer
The build bundles the code the browser needs into dist. Packages like vite are only used during the build, and React is already included in the bundle.