question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not working with vitejs in production mode

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jclohmanncommented, Aug 12, 2021

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:

import CookieConsent from 'react-cookie-consent';

console.log(CookieConsent);

Which gave me this output just before the described error:

Module {…}
	Cookies: ƒ o()
	OPTIONS: Object
	SAME_SITE_OPTIONS: Object
	default: ƒ i(e)
	getCookieConsentValue: ƒ ()
	Symbol(Symbol.toStringTag): "Module"
	__esModule: true
	get Cookies: ƒ ()
	get OPTIONS: ƒ ()
	get SAME_SITE_OPTIONS: ƒ ()
	get default: ƒ ()
	get getCookieConsentValue: ƒ ()

It seems the CookieConsent-component is not available as default but as a property named default.

As a workaround one can use the default properties value as a component by doing this:

import CookieConsentX from 'react-cookie-consent';

// @ts-ignore
const CookieConsent = CookieConsentX.default;

const SomeReactComponent = () => {
	return (
		<CookieConsent ... />
	);
};

This is not ideal when using typescript, since this replaces the correct type with any. (Which can surely be fixed using as.)

Another way I found was to change react-cookie-consent’s webpack-config like this:

-    // libraryTarget: "commonjs2", // THIS IS THE MOST IMPORTANT LINE! :mindblow: I wasted more than 2 days until realize this was the line most important in all this guide.
+    library: {
+      name: "reactCookieConsent",
+      type: 'umd'
+    },

It works as expected with this config.

@Mastermindzh Do you think we can change the library-type to umd without issues?

2reactions
Mastermindzhcommented, Mar 19, 2021

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 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found