Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ The supported template presets are:
## Scaffolding Your First Vite Project

::: tip Compatibility Note
Vite requires [Node.js](https://nodejs.org/en/) version 20+. However, some templates require a higher Node.js version to work, please upgrade if your package manager warns about it.
Vite requires [Node.js](https://nodejs.org/en/) version 20.19+, 22.12+. However, some templates require a higher Node.js version to work, please upgrade if your package manager warns about it.
:::

::: code-group
Expand Down
46 changes: 15 additions & 31 deletions docs/guide/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,6 @@ See [Rollup's troubleshooting guide](https://rollupjs.org/troubleshooting/) for

If the suggestions here don't work, please try posting questions on [GitHub Discussions](https://github.com/vitejs/vite/discussions) or in the `#help` channel of [Vite Land Discord](https://chat.vite.dev).

## CJS

### Vite CJS Node API deprecated

The CJS build of Vite's Node API is deprecated and will be removed in Vite 6. See the [GitHub discussion](https://github.com/vitejs/vite/discussions/13928) for more context. You should update your files or frameworks to import the ESM build of Vite instead.

In a basic Vite project, make sure:

1. The `vite.config.js` file content is using the ESM syntax.
2. The closest `package.json` file has `"type": "module"`, or use the `.mjs`/`.mts` extension, e.g. `vite.config.mjs` or `vite.config.mts`.

For other projects, there are a few general approaches:

- **Configure ESM as default, opt-in to CJS if needed:** Add `"type": "module"` in the project `package.json`. All `*.js` files are now interpreted as ESM and need to use the ESM syntax. You can rename a file with the `.cjs` extension to keep using CJS instead.
- **Keep CJS as default, opt-in to ESM if needed:** If the project `package.json` does not have `"type": "module"`, all `*.js` files are interpreted as CJS. You can rename a file with the `.mjs` extension to use ESM instead.
- **Dynamically import Vite:** If you need to keep using CJS, you can dynamically import Vite using `import('vite')` instead. This requires your code to be written in an `async` context, but should still be manageable as Vite's API is mostly asynchronous.

If you're unsure where the warning is coming from, you can run your script with the `VITE_CJS_TRACE=true` flag to log the stack trace:

```bash
VITE_CJS_TRACE=true vite dev
```

If you'd like to temporarily ignore the warning, you can run your script with the `VITE_CJS_IGNORE_WARNING=true` flag:

```bash
VITE_CJS_IGNORE_WARNING=true vite dev
```

Note that postcss config files do not support ESM + TypeScript (`.mts` or `.ts` in `"type": "module"`) yet. If you have postcss configs with `.ts` and added `"type": "module"` to package.json, you'll also need to rename the postcss config to use `.cts`.

## CLI

### `Error: Cannot find module 'C:\foo\bar&baz\vite\bin\vite.js'`
Expand Down Expand Up @@ -245,3 +214,18 @@ An example of cross drive links are:
- a symlink/junction to a different drive by `mklink` command (e.g. Yarn global cache)

Related issue: [#10802](https://github.com/vitejs/vite/issues/10802)

<script setup lang="ts">
// redirect old links with hash to old version docs
if (typeof window !== "undefined") {
const hashForOldVersion = {
'vite-cjs-node-api-deprecated': 6
}

const version = hashForOldVersion[location.hash.slice(1)]
if (version) {
// update the scheme and the port as well so that it works in local preview (it is http and 4173 locally)
location.href = `https://v${version}.vite.dev` + location.pathname + location.search + location.hash
}
}
</script>
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default tseslint.config(
},
settings: {
node: {
version: '^20.0.0 || >=22.0.0',
version: '^20.19.0 || >=22.12.0',
},
},
plugins: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"type": "module",
"engines": {
"node": "^20.0.0 || >=22.0.0"
"node": "^20.19.0 || >=22.12.0"
},
"homepage": "https://vite.dev/",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-vite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Scaffolding Your First Vite Project

> **Compatibility Note:**
> Vite requires [Node.js](https://nodejs.org/en/) version 20+. However, some templates require a higher Node.js version to work, please upgrade if your package manager warns about it.
> Vite requires [Node.js](https://nodejs.org/en/) version 20.19+, 22.12+. However, some templates require a higher Node.js version to work, please upgrade if your package manager warns about it.

With NPM:

Expand Down
2 changes: 1 addition & 1 deletion packages/create-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"prepublishOnly": "npm run build"
},
"engines": {
"node": "^20.0.0 || >=22.0.0"
"node": "^20.19.0 || >=22.12.0"
},
"repository": {
"type": "git",
Expand Down
15 changes: 3 additions & 12 deletions packages/plugin-legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,14 @@
"vite-plugin",
"@vitejs/plugin-legacy"
],
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"exports": "./dist/index.js",
"scripts": {
"dev": "tsdown --watch",
"build": "tsdown && pnpm run patch-cjs",
"patch-cjs": "tsx ../../scripts/patchCJS.ts",
"build": "tsdown",
"prepublishOnly": "npm run build"
},
"engines": {
"node": "^20.0.0 || >=22.0.0"
"node": "^20.19.0 || >=22.12.0"
},
"repository": {
"type": "git",
Expand Down
23 changes: 13 additions & 10 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -987,22 +987,25 @@ function wrapIIFEBabelPlugin(): BabelPlugin {
}
}

const hash =
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- crypto.hash is supported in Node 21.7.0+, 20.12.0+
crypto.hash ??
((
algorithm: string,
data: crypto.BinaryLike,
outputEncoding: crypto.BinaryToTextEncoding,
) => crypto.createHash(algorithm).update(data).digest(outputEncoding))

export const cspHashes = [
safari10NoModuleFix,
systemJSInlineCode,
detectModernBrowserCode,
dynamicFallbackInlineCode,
].map((i) => hash('sha256', i, 'base64'))
].map((i) => crypto.hash('sha256', i, 'base64'))

export type { Options }

export default viteLegacyPlugin

// Compat for require
function viteLegacyPluginCjs(this: unknown, options: Options): Plugin[] {
return viteLegacyPlugin.call(this, options)
}
Object.assign(viteLegacyPluginCjs, {
cspHashes,
default: viteLegacyPluginCjs,
detectPolyfills,
})

export { viteLegacyPluginCjs as 'module.exports' }
8 changes: 1 addition & 7 deletions packages/plugin-legacy/tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import { defineConfig } from 'tsdown'

export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
target: 'node20',
tsconfig: false, // disable tsconfig `paths` when bundling
outputOptions(opts, format) {
if (format === 'cjs') {
opts.exports = 'named'
}
return opts
},
dts: true,
})
96 changes: 0 additions & 96 deletions packages/vite/index.cjs

This file was deleted.

6 changes: 0 additions & 6 deletions packages/vite/index.d.cts

This file was deleted.

10 changes: 3 additions & 7 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
"main": "./dist/node/index.js",
"types": "./dist/node/index.d.ts",
"exports": {
".": {
"module-sync": "./dist/node/index.js",
"import": "./dist/node/index.js",
"require": "./index.cjs"
},
".": "./dist/node/index.js",
"./client": {
"types": "./client.d.ts"
},
Expand Down Expand Up @@ -58,7 +54,7 @@
"types"
],
"engines": {
"node": "^20.0.0 || >=22.0.0"
"node": "^20.19.0 || >=22.12.0"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -156,7 +152,7 @@
"ws": "^8.18.2"
},
"peerDependencies": {
"@types/node": "^20.0.0 || >=22.0.0",
"@types/node": "^20.19.0 || >=22.12.0",
"jiti": ">=1.21.0",
"less": "^4.0.0",
"lightningcss": "^1.21.0",
Expand Down
50 changes: 0 additions & 50 deletions packages/vite/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,32 +169,11 @@ const moduleRunnerConfig = defineConfig({
},
})

const cjsConfig = defineConfig({
...sharedNodeOptions,
input: {
publicUtils: path.resolve(__dirname, 'src/node/publicUtils.ts'),
},
output: {
...sharedNodeOptions.output,
entryFileNames: `node-cjs/[name].cjs`,
chunkFileNames: 'node-cjs/chunks/dep-[hash].js',
format: 'cjs',
target: 'node20',
},
external: ['fsevents', 'supports-color', ...Object.keys(pkg.dependencies)],
plugins: [
bundleSizeLimit(120),
exportCheck(),
externalizeDepsInWatchPlugin(),
],
})

export default defineConfig([
envConfig,
clientConfig,
nodeConfig,
moduleRunnerConfig,
cjsConfig,
])

// #region Plugins
Expand Down Expand Up @@ -430,33 +409,4 @@ function bundleSizeLimit(limit: number): Plugin {
}
}

function exportCheck(): Plugin {
return {
name: 'export-check',
async writeBundle() {
if (this.meta.watchMode) return

// escape import so that it's not bundled while config load
const dynImport = (id: string) => import(id)
// ignore warning from CJS entrypoint to avoid misleading logs
process.env.VITE_CJS_IGNORE_WARNING = 'true'

const esmNamespace = await dynImport('./dist/node/index.js')
const cjsModuleExports = (await dynImport('./index.cjs')).default
const cjsModuleExportsKeys = new Set(
Object.getOwnPropertyNames(cjsModuleExports),
)
const lackingExports = Object.keys(esmNamespace).filter(
(key) => !cjsModuleExportsKeys.has(key),
)
if (lackingExports.length > 0) {
this.error(
`Exports missing from cjs build: ${lackingExports.join(', ')}.` +
` Please update index.cjs or src/publicUtils.ts.`,
)
}
},
}
}

// #endregion
Loading