Not working with vitejs in production mode
See original GitHub issueI tried to use react-cookie-consent in a vitejs app. It works in development mode but in production, react throws me this error:
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
I’ve joined a test project to reproduce it. test-cookie.tar.gz.
It’s a vitejs project from scratch with the minimal modifications (importing and using the lib) to reproduce the bug.
You can test it by executing:
npm ci
npm run dev # It's working on localhost:3000
npm run build
npm run serve # It's broken on localhost:5000
I suspect a compatibility issue with imports…
Thanks for your project.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Can't build app in production mode · Issue #1182 · vitejs/vite
My problem has gone when I tried to build vite program from a real directory, not from a directory which was a symlink...
Read more >Building for Production - Vite
In library mode, all import.meta.env.* usage are statically replaced when building for production. However, process.env.* usage are not, so that consumers of ...
Read more >Troubleshooting - Vite
Error: Cannot find module 'C:\foo\bar&baz\vite\bin\vite.js' #. The path to your project folder may include & , which doesn't work with npm on Windows...
Read more >Env Variables and Modes - Vite
For example, dynamic key access like import.meta.env[key] will not work. ... An env file for a specific mode (e.g. .env.production ) will take...
Read more >Deploying a Static Site - Vite
You may run npm run build command to build the app. ... It's an easy way to check if the production build looks...
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 Free
Top 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

Hi! I am also kind of stuck in this problem. There seems to be a problem with the generated default-export. Logging the imported component gave me this output:
Which gave me this output just before the described error:
It seems the CookieConsent-component is not available as default but as a property named
default.As a workaround one can use the
defaultproperties value as a component by doing this:This is not ideal when using typescript, since this replaces the correct type with
any. (Which can surely be fixed usingas.)Another way I found was to change react-cookie-consent’s webpack-config like this:
It works as expected with this config.
@Mastermindzh Do you think we can change the library-type to umd without issues?
Hey @jrmi ,
I have no experience with Vitejs but I will try to find out what is wrong next week. Don’t have much time left this week, sorry 😃