Vite not working with `"type": "module"` in `package.json`
See original GitHub issueDescribe the bug
I am trying to create a Node project with "type": "module"
.
I’ve ported my config files from RequireJS module.exports
to modern ES modules export default
syntax.
On the console, I am seeing errors like the following:
1:20:56 PM [vite] Internal server error: Failed to load PostCSS config (searchPath: C:/workspace/muffi.net/src/MuffiNet.FrontendReact/ClientApp): [Error] require() of ES Module C:\workspace\muffi.net\src\MuffiNet.FrontendReact\ClientApp\postcss.config.js from C:\workspace\muffi.net\src\MuffiNet.FrontendReact\ClientApp\node_modules\vite\dist\node\chunks\dep-c9998dc6.js not supported.
Instead change the require of postcss.config.js in C:\workspace\muffi.net\src\MuffiNet.FrontendReact\ClientApp\node_modules\vite\dist\node\chunks\dep-c9998dc6.js to a dynamic import() which is available in all CommonJS modules.
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\workspace\muffi.net\src\MuffiNet.FrontendReact\ClientApp\postcss.config.js from C:\workspace\muffi.net\src\MuffiNet.FrontendReact\ClientApp\node_modules\vite\dist\node\chunks\dep-c9998dc6.js not supported.
Instead change the require of postcss.config.js in C:\workspace\muffi.net\src\MuffiNet.FrontendReact\ClientApp\node_modules\vite\dist\node\chunks\dep-c9998dc6.js to a dynamic import() which is available in all CommonJS modules.
at Object.search (C:\workspace\muffi.net\src\MuffiNet.FrontendReact\ClientApp\node_modules\vite\dist\node\chunks\dep-c9998dc6.js:22845:43)
at async resolvePostcssConfig (C:\workspace\muffi.net\src\MuffiNet.FrontendReact\ClientApp\node_modules\vite\dist\node\chunks\dep-c9998dc6.js:37590:22)
at async compileCSS (C:\workspace\muffi.net\src\MuffiNet.FrontendReact\ClientApp\node_modules\vite\dist\node\chunks\dep-c9998dc6.js:37361:27)
at async TransformContext.transform (C:\workspace\muffi.net\src\MuffiNet.FrontendReact\ClientApp\node_modules\vite\dist\node\chunks\dep-c9998dc6.js:37010:55)
at async Object.transform (C:\workspace\muffi.net\src\MuffiNet.FrontendReact\ClientApp\node_modules\vite\dist\node\chunks\dep-c9998dc6.js:39317:30)
at async doTransform (C:\workspace\muffi.net\src\MuffiNet.FrontendReact\ClientApp\node_modules\vite\dist\node\chunks\dep-c9998dc6.js:50037:29)
It looks like require
statements are baked into vite\dist\node\chunks\dep-c9998dc6.js
?
Reproduction
https://github.com/mindplay-dk/vite-tailwind-issue/commit/60e31c782991d5f4ce4c887cb770c7931f3c02ad
System Info
System:
OS: Windows 10 10.0.19044
CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
Memory: 19.25 GB / 31.73 GB
Binaries:
Node: 16.15.1 - C:\nodejs\node.EXE
Yarn: 1.22.19 - C:\nodejs\yarn.CMD
npm: 8.11.0 - C:\nodejs\npm.CMD
Browsers:
Edge: Spartan (44.19041.1266.0), Chromium (103.0.1264.49)
Internet Explorer: 11.0.19041.1566
npmPackages:
@vitejs/plugin-react: ^1.3.2 => 1.3.2
vite: ^2.9.14 => 2.9.14
Used Package Manager
yarn
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created a year ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Why Vite Cli adds type:module to package.json?
type : module in package.json makes a lot of incompatibility in my project. My question is can I remove type: module without any...
Read more >Configuring Vite
Note Vite supports using ES modules syntax in the config file even if the project is not using native Node ESM, e.g. type:...
Read more >Getting Started - Vite
npm 6.x npm create vite@latest my-vue-app --template vue # npm 7+, ... so you can write code as if you are working with...
Read more >Troubleshooting - Vite
Syntax Error / Type Error happens # ... Vite cannot handle and does not support code that only runs on non-strict mode (sloppy...
Read more >Building for Production | Vite (main branch)
Running vite build with this config uses a Rollup preset that is oriented towards ... If the package.json does not contain "type": "module"...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks! I’m on vacation, but I will give this a shot when I’m back at work in a couple of weeks. 🙂
It worked after changing:
"target": "es5"
to"target": "es2015"
intsconfig.js
.tailwind.config.js
to CJStailwind.config.cjs
export default config
tomodule.exports = config
It seems tailwind does not support native ESM. Closing as it is already fixed on Vite side and the error is coming from tailwind.