I am having issue which says Uncaught ReferenceError: require is not defined. I am using React 18, Vite version 5. I implemented Vite to my existing React project. Everything working in dev, but when I build and deploy it into production, I am having this error. Can anyone help me with it?
I searched everywhere but no success.
1. Importing modules
When using Vite, use the
importsyntax instead ofrequire()2. Referencing assets:
Also, when including images, svgs and videos, replace the
require()you used to use in create-react-app with the actual path of the asset file.Example:
3. Importing assets
Sometimes, in your code, you import the assets as modules:
The same happens in Vite.
This is useful when you use path aliases, for instance, sometimes, you import the images in create-react-app like this:
In vite, if you wanted that, import it first at the top then use it at the bottom:
Summary: there's nothing called
require()in vite, if you used to do that in create-react-app, it can't happen in Vite.