Skip to content

Commit 404f30f

Browse files
authored
feat!: deprecate cjs node api (#14278)
1 parent 881d080 commit 404f30f

File tree

6 files changed

+68
-5
lines changed

6 files changed

+68
-5
lines changed

docs/guide/troubleshooting.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,35 @@ See [Rollup's troubleshooting guide](https://rollupjs.org/troubleshooting/) for
44

55
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.vitejs.dev).
66

7+
## CJS
8+
9+
### Vite CJS Node API deprecated
10+
11+
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.
12+
13+
In a basic Vite project, make sure:
14+
15+
1. The `vite.config.js` file content is using the ESM syntax.
16+
2. The closest `package.json` file has `"type": "module"`, or use the `.mjs` extension, e.g. `vite.config.mjs`.
17+
18+
For other projects, there are a few general approaches:
19+
20+
- **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 needs to use the ESM syntax. You can rename a file with the `.cjs` extension to keep using CJS instead.
21+
- **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.
22+
- **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.
23+
24+
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:
25+
26+
```bash
27+
VITE_CJS_TRACE=true vite dev
28+
```
29+
30+
If you'd like to temporarily ignore the warning, you can run your script with the `VITE_CJS_IGNORE_WARNING=true` flag:
31+
32+
```bash
33+
VITE_CJS_IGNORE_WARNING=true vite dev
34+
```
35+
736
## CLI
837

938
### `Error: Cannot find module 'C:\foo\bar&baz\vite\bin\vite.js'`

packages/vite/index.cjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable no-restricted-globals */
22

3+
warnCjsUsage()
4+
35
// type utils
46
module.exports.defineConfig = (config) => config
57

@@ -32,3 +34,15 @@ unsupportedCJS.forEach((name) => {
3234
)
3335
}
3436
})
37+
38+
function warnCjsUsage() {
39+
if (process.env.VITE_CJS_IGNORE_WARNING) return
40+
globalThis.__vite_cjs_skip_clear_screen = true
41+
const yellow = (str) => `\u001b[33m${str}\u001b[39m`
42+
const log = process.env.VITE_CJS_TRACE ? console.trace : console.warn
43+
log(
44+
yellow(
45+
`The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.`,
46+
),
47+
)
48+
}

packages/vite/index.d.cts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @deprecated The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.
3+
*/
4+
declare const module: any
5+
6+
export = module

packages/vite/package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020
"types": "./dist/node/index.d.ts",
2121
"exports": {
2222
".": {
23-
"types": "./dist/node/index.d.ts",
24-
"import": "./dist/node/index.js",
25-
"require": "./index.cjs"
23+
"import": {
24+
"types": "./dist/node/index.d.ts",
25+
"default": "./dist/node/index.js"
26+
},
27+
"require": {
28+
"types": "./index.d.cts",
29+
"default": "./index.cjs"
30+
}
2631
},
2732
"./client": {
2833
"types": "./client.d.ts"
@@ -38,6 +43,7 @@
3843
"dist",
3944
"client.d.ts",
4045
"index.cjs",
46+
"index.d.cts",
4147
"types"
4248
],
4349
"engines": {

packages/vite/src/node/cli.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,11 @@ cli
185185
`\n ${colors.green(
186186
`${colors.bold('VITE')} v${VERSION}`,
187187
)} ${startupDurationString}\n`,
188-
{ clear: !server.config.logger.hasWarned },
188+
{
189+
clear:
190+
!server.config.logger.hasWarned &&
191+
!(globalThis as any).__vite_cjs_skip_clear_screen,
192+
},
189193
)
190194

191195
server.printUrls()

vitest.config.e2e.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export default defineConfig({
2121
moduleDirectories: ['node_modules', 'packages'],
2222
},
2323
onConsoleLog(log) {
24-
if (log.match(/experimental|jit engine|emitted file|tailwind/i))
24+
if (
25+
log.match(
26+
/experimental|jit engine|emitted file|tailwind|The CJS build of Vite/i,
27+
)
28+
)
2529
return false
2630
},
2731
},

0 commit comments

Comments
 (0)