Sub topic 2
Start from a Vite project
This chapter assumes you can run commands in a terminal, have Node.js and npm installed, and know that Vite is a dev server and bundler. By the end you will have a working Vite project with no Tailwind in it yet. You will also know which files matter for the next chapters and which problems to rule out first.
Why start with a plain project
Tailwind v4 is added to Vite as a plugin. If the project does not work before you add the plugin, you cannot tell whether a later failure comes from Tailwind or from your setup. So you first build a baseline that runs, then change one thing at a time.
The baseline has three jobs:
- Prove that Node, npm, and Vite work on your machine.
- Give you an entry HTML file, a JavaScript entry file, and a CSS file. Later chapters edit all three.
- Give you a known-good state to compare against when something breaks.
Check your Node.js version
Vite runs on Node.js, and each Vite release sets a minimum version. Recent releases require Node 20.19 or later, or 22.12 or later. Older Vite releases accepted older Node versions. Check yours first:
node --version
npm --versionIf the Node version is below what Vite asks for, the scaffold command or the dev server may fail with a syntax error or an "unsupported engine" message. Upgrade Node before going further. A version manager such as nvm, fnm, or Volta makes this easy.
The Tailwind plugin has its own requirement: it needs a reasonably recent Vite (5.2 or later). A project created today with vite@latest meets it.
Create the project
The scaffolding command is npm create vite@latest. It downloads the latest create-vite package and runs it.
npm create vite@latest my-appThe command asks you questions in the terminal:
- Project name. This becomes the folder name. You already passed
my-app, so it is skipped. - Framework. Choices include Vanilla, Vue, React, Preact, Lit, Svelte, Solid, and others.
- Variant. For most frameworks you choose between JavaScript and TypeScript.
This tutorial's examples use the Vanilla framework with the JavaScript variant, because it has the least code to read. The Tailwind setup is the same for every framework, since the plugin works on CSS and does not depend on your UI library.
Skip the prompts
For scripts, CI, or a repeatable setup, pass the template yourself. With npm 7 or later, put an extra -- before the template flag so npm forwards it to create-vite:
npm create vite@latest my-app -- --template vanillaOther template names follow the same pattern: vanilla-ts, react, react-ts, vue, vue-ts, svelte, and so on.
Scaffold into an existing folder
If you already created and entered the folder, use . as the name:
npm create vite@latest . -- --template vanillaVite asks what to do if the folder is not empty. Choose carefully. Overwriting can replace files such as index.html and package.json. In a folder with uncommitted work, commit first.
Other package managers
Every package manager has an equivalent command.
| Package manager | Scaffold command |
|---|---|
| npm | `npm create vite@latest my-app` |
| pnpm | `pnpm create vite my-app` |
| Yarn | `yarn create vite my-app` |
| Bun | `bun create vite my-app` |
The rest of this tutorial uses npm. If you use another tool, swap npm install for the matching install command. Use one package manager per project, because mixing them leaves several lockfiles that disagree.
Install dependencies and run it
The scaffold only writes files. It does not install anything. Enter the folder, install, and start the dev server:
cd my-app
npm install
npm run devVite prints a local address, usually http://localhost:5173/. Open it. The vanilla template shows a Vite logo and a counter button. Click the button to confirm that JavaScript runs.
If port 5173 is busy, Vite picks the next free port, such as 5174, and prints it. Always read the address from the terminal instead of assuming it. Stop the server with Ctrl+C.
Read the project layout
The exact files depend on the template version, but a vanilla project looks like this:
my-app/
├── index.html
├── package.json
├── public/
│ └── vite.svg
└── src/
├── counter.js
├── main.js
└── style.cssOlder template versions place main.js, counter.js, and style.css in the project root instead of in src/. If your layout differs, nothing breaks. Use your own paths wherever later chapters say src/.
index.html is the real entry point
In many build tools the JavaScript file is the entry. In Vite, index.html is the entry, and it sits at the project root, not in public/. It loads your JavaScript with a module script tag:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>my-app</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>Two details matter later:
<div id="app">is where the vanilla template renders. The Tailwind verification step in a later chapter writes a heading into it.- The path
/src/main.jsstarts with/, which Vite resolves from the project root.
main.js pulls in the CSS
In the vanilla template, main.js imports style.css at the top. In Vite, you import a CSS file from JavaScript, and Vite injects it into the page in development and extracts it into a separate file in production builds. No <link> tag is needed. This is the mechanism you will rely on to load Tailwind.
The third arrow in the diagram is where Tailwind will take part. When the plugin is registered, Vite hands your CSS to it before the browser sees the result.
package.json and scripts
Open package.json. Version numbers on your machine will differ.
{
"name": "my-app",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"vite": "^7.0.0"
}
}"type": "module"makes Node treat.jsfiles as ES modules. This is why a config file can useimportsyntax, as the next chapters require.devstarts the dev server.buildmakes the production output indist/.previewserves that output locally so you can check it.- Vite is a dev dependency. Tailwind will be added alongside it.
No config file yet
A fresh vanilla project has no vite.config file. Vite runs on defaults without one. You will create vite.config.ts when you register the Tailwind plugin. vite.config.js also works, and the TypeScript version works even in a JavaScript project because Vite compiles the config itself.
Common problems at this stage
npm create vitehangs or fails to fetch. Check your network, proxy, and registry settings withnpm config get registry.- Syntax errors on start. Node is probably too old. Re-check
node --versionagainst the Vite requirement. - "Missing script: dev". You are in the wrong folder. Run
pwdordirand make surepackage.jsonis in the current directory. - Blank page and no counter. Open the browser console. A 404 on
/src/main.jsmeans the script path inindex.htmldoes not match where the file lives. - A different port opens. Another process holds 5173. This is expected behavior, not an error.
What you have now
You have a Vite project that installs, starts, and serves a page. You know that index.html loads main.js, that main.js imports style.css, and that there is no Vite config yet. The next chapter installs tailwindcss and @tailwindcss/vite into this project.
Exercise
You scaffolded a Vite project into an existing folder, and npm run dev fails with "Missing script: dev". Name two likely causes and how to check each one.
Show answer
First, you may be running the command in the wrong directory. Check that package.json exists in the current folder, for example with ls or dir. Second, the scaffold may not have written the files you expected, for example because you declined to overwrite a non-empty folder, so an old package.json without a dev script is still in place. Open package.json and check that its scripts section contains "dev": "vite".
Exercise
In a Vite project, which file is the entry point, and how does a CSS file such as style.css end up applied to the page without a <link> tag?
Show answer
index.html at the project root is the entry point. It loads main.js with a <script type="module"> tag. main.js imports style.css, and Vite injects the processed CSS into the page during development and extracts it into a CSS file in production builds.