Sub topic 9
Checking Node.js and npm
This chapter assumes you can use a terminal, know basic JavaScript including ES modules, and understand what a package manager does in general. You don't need React yet. The goal is to confirm that your machine can run npm create vite@latest in the next chapter, and to fix the usual problems before they get in your way.
Why Vite needs Node.js
Vite is a JavaScript program. It runs on Node.js, a runtime that executes JavaScript outside the browser. When you run the dev server or a production build, Node runs Vite, and Vite serves or bundles your React code for the browser.
npm is the package manager that ships with Node.js. It does three jobs in this tutorial:
- It downloads the project scaffolding tool (
create-vite). - It installs your dependencies, such as
react,react-dom, andvite, into anode_modulesfolder. - It runs the scripts defined in
package.json, such asnpm run devandnpm run build.
Node and npm are two programs installed together. Each has its own version number, so you check both.
Checking what you have installed
Open a terminal and run:
node --version
npm --versionTypical output looks like this (your numbers will differ):
v22.12.0
10.9.0If either command prints "command not found" or "not recognized", Node.js is not installed, or it is installed but not on your PATH. The PATH is the list of folders your shell searches when you type a command.
Which Node version you need
Vite sets a minimum Node.js version, and that minimum changes between Vite releases. Recent Vite versions require an even-numbered, long-term support (LTS) release, and they drop support for Node versions that have reached end of life. Check the current requirement in the Vite documentation under "Getting Started" before you rely on an old install.
Node release lines follow a pattern:
| Release type | Version numbers | Use it for |
|---|---|---|
| LTS (long-term support) | Even majors, such as 20 and 22 | Projects you want to keep working |
| Current | Latest major, odd or even | Trying new features |
| End of life | Old majors | Nothing. They no longer get security fixes. |
If node --version prints a version older than Vite's minimum, npm create vite may fail, or the dev server may crash with a syntax error inside Vite's own files. The cause is the old runtime. Your code isn't at fault.
Installing or upgrading Node.js
You have two common options.
- The official installer. Download the LTS installer from nodejs.org and run it. This is the simplest path, but it installs one global version.
- A version manager. Tools such as nvm (macOS and Linux), nvm-windows, fnm, or Volta let you install several Node versions and switch between them per project.
A version manager is worth the extra setup if you work on more than one project, because different projects often need different Node versions. With nvm, for example:
nvm install --lts
nvm use --lts
node --versionAfter installing or switching, open a new terminal window. An already-open shell may keep the old PATH, which is a common reason node --version still shows the old number.
How the pieces fit together
The command goes to npm. npm starts Node.js with the script from package.json. Node runs Vite, and Vite sends your app to the browser.
Checking that npm works
Checking the version tells you the program exists. It doesn't prove npm can reach the package registry, the server where packages are stored. Test that with a command that reads from the registry without changing anything:
npm view vite versionThis prints the latest published version of Vite, for example 6.0.1. If it hangs or fails, see the troubleshooting list below.
You can also see where npm is configured to look:
npm config get registryThe default is https://registry.npmjs.org/. If you work behind a company proxy or mirror, this may point somewhere else, and that is usually intentional.
npx, `npm create`, and what the next chapter runs
npm create vite@latest is shorthand. npm adds the create- prefix and runs the package create-vite at its latest version. It downloads the package to a temporary cache, runs it, and doesn't install it globally. That is why you don't need to install anything before scaffolding, and why the @latest tag matters: it asks for the newest template instead of a cached older one.
The related tool npx runs a package's command in the same way. You will see it in older tutorials. For this tutorial, npm create is the form to use.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| `node: command not found` | Node is not installed, or not on `PATH` | Install Node, then open a new terminal |
| Version is lower than Vite requires | Old install | Upgrade to the current LTS |
| Version doesn't change after upgrading | Old shell, or a second Node install earlier on `PATH` | Open a new terminal. Run `which node` (macOS and Linux) or `where node` (Windows) to see which one runs. |
| `EACCES` permission error on install | npm is writing to a system-owned folder | Use a version manager, or change npm's global directory. Don't use `sudo npm`. |
| `ETIMEDOUT` or `ENOTFOUND` | No network access, or a proxy blocks the registry | Check your connection and `npm config get registry` |
| `npm` works but `node` is missing (or the reverse) | Partial or mixed installs | Reinstall from one source |
Optional: pinning a Node version per project
If your team needs everyone on the same Node version, you can record it in the project. Two common ways are a .nvmrc file, which nvm and fnm read, and the engines field in package.json:
{
"name": "my-app",
"private": true,
"engines": {
"node": ">=20.19.0"
}
}By default npm only warns when engines is not satisfied. It doesn't stop the install unless engine-strict is turned on. The version number above is an example, so use the minimum that your Vite version states.
Checklist before moving on
node --versionprints a supported LTS version.npm --versionprints a version.npm view vite versionprints a version number.- You know how to open a fresh terminal after changing Node versions.
Exercise
You upgraded Node.js, but node --version in your terminal still prints the old version. Name two likely causes and how you would check each one.
Show answer
One cause is that the terminal was opened before the upgrade and still has the old PATH. Open a new terminal and run node --version again. The other cause is that a second Node install appears earlier on PATH. Run which node (macOS and Linux) or where node (Windows) to see which executable runs, then remove the old install or fix the order of PATH. With a version manager, also run something like nvm use --lts to select the new version.
Exercise
node --version and npm --version both work, but npm create vite@latest fails with ENOTFOUND. What does this tell you, and what command would you run to investigate?
Show answer
Node and npm are installed, so the problem is network access to the package registry, such as no connection, a DNS failure, or a proxy. Run npm config get registry to see which registry npm uses, and npm view vite version to test whether npm can reach it.