Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"The CJS build of Vite's Node API is deprecated" when using a TS vite.config.ts and the package.json has no type #15010

Closed
7 tasks done
xaviergonz opened this issue Nov 16, 2023 · 4 comments · Fixed by #15017
Labels
documentation Improvements or additions to documentation

Comments

@xaviergonz
Copy link

Describe the bug

I just updated to vite 5 and I am using a vite.config.ts file, but when compiling the following shows in the console:

The CJS build of Vite's Node API is deprecated

In this case the package.json has no "type": "module" set. If that's is there the warning does not appear.

If I'm not mistaken, Vite is the one that internally compiles that file from TS into CJS instead of ESM, or is there something I need to do to make that happen without having to set the type of the whole package to module?

Reproduction

https://stackblitz.com/edit/vitejs-vite-kn2wsz?file=package.json&view=editor

Steps to reproduce

npm install
npm run build

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.9.2 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^5.0.0 => 5.0.0

Used Package Manager

npm

Logs

No response

Validations

@xaviergonz
Copy link
Author

PD: I just noticed that if I rename the file to vite.config.mts the error goes away

@ghiscoding
Copy link
Contributor

That is mentioned in Deprecate CJS Node API

In a basic Vite project, make sure:

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

There's more info to read about, the link above is also available in the console warning if I remember correctly

@xaviergonz
Copy link
Author

I read it and while it does mention js and mjs, it doesn't say anything specific about ts / mts

@bluwy
Copy link
Member

bluwy commented Nov 17, 2023

We can add a note about ts variants. I'll send a PR for it. But yes, you need vite.config.mts in this case. If you use .ts and no type: "module", it implies the runtime is CJS so Vite compiles to CJS instead.

@bluwy bluwy added documentation Improvements or additions to documentation and removed pending triage labels Nov 17, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants