Replies: 10 comments 6 replies
-
I just found Issue #2139 from back in February that discusses this problem. Unfortunately, it's November and there's still no practical solution in sight. As others stated in #2139, migrating from Create React App (CRA) to ViteJS seems not possible. I am puzzled how ViteJS can be used at all for React without hacking all kinds of popular packages. 🤔 If anybody knows how to identify the (one, or potentially many) offending libraries that exist in a mature CRA project along with a clean workaround, please advise. Unfortunately after a few days attempting to migrate to ViteJS, it looks like rolling back CRA with slow-n-painful builds is the only choice. 😢 |
Beta Was this translation helpful? Give feedback.
-
I managed to solve this for our project by adding the import react from '@vitejs/plugin-react';
export default defineConfig(({ mode }) => {
return {
resolve: {
alias: {
// I needed this to make dev mode work.
'react/jsx-runtime': 'react/jsx-runtime.js',
},
},
plugins: [react({
babel: {
plugins: [
['@babel/plugin-transform-react-jsx', { runtime: 'automatic' }],
]
},
})],
};
}); Click to expand for my full config!import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react';
import commonjs from '@rollup/plugin-commonjs';
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, 'env');
const htmlPlugin = () => {
return {
name: 'html-transform',
// Replace %VITE_X% env variables in index.html.
transformIndexHtml(html: string) {
return html.replace(/%(.*?)%/g, function match(_match, p1) {
return env[p1];
});
},
};
};
return {
resolve: {
alias: {
'react/jsx-runtime': 'react/jsx-runtime.js',
},
},
define: {
'process.env': process.env,
},
build: {
outDir: './build',
rollupOptions: {
plugins: [commonjs()],
},
commonjsOptions: {
exclude: [/./],
},
},
publicDir: './public',
plugins: [htmlPlugin(), react({
babel: {
plugins: [
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: false }],
'babel-plugin-transform-typescript-metadata',
'babel-plugin-parameter-decorator',
['@babel/plugin-transform-react-jsx', { runtime: 'automatic' }],
]
},
})],
server: {
fs: {
// Allow serving files from workspace root, the fonts are for some reason fetched from there!
allow: ['../..'],
},
},
};
}); I hope that it also could work for you, or someone else with the same problem. |
Beta Was this translation helpful? Give feedback.
-
I solved this by doing something like this: By exporting as default and then importing as |
Beta Was this translation helpful? Give feedback.
-
I was able to solve mine removing NODE_ENV=noMatterTheValue from .env files |
Beta Was this translation helpful? Give feedback.
-
I had the same problem but it looks like it was caused by some packages, in my case it was react-phone-input-2 I removed it and used other input and works ( no error in production) |
Beta Was this translation helpful? Give feedback.
-
So I did stumble across this issue today with getting a production react build working with SSR. The issue exists in the I found this in the plugins typescript file: /**
* Set this to `"automatic"` to use [vite-react-jsx](https://github.com/alloc/vite-react-jsx).
* @default "automatic"
*/
jsxRuntime?: 'classic' | 'automatic'; By default, the config is set to This appears to be something they fixed for the next major version as the Anyway, the plugins: [react({ jsxRuntime: 'classic' })], or use the suggestion above related to the Babel Transform. This is also listed under the opt-out for the config: https://www.npmjs.com/package/@vitejs/plugin-react Also, for those curious about the difference in runtimes: |
Beta Was this translation helpful? Give feedback.
-
My solution: Create an env file in the root of the project.: Add the NODE_ENV to it: Save and build the solution. I hope it helps. |
Beta Was this translation helpful? Give feedback.
-
i am still getting the error |
Beta Was this translation helpful? Give feedback.
-
@janderson099 's solution works, but Vite gives the warning My the solution was to rename |
Beta Was this translation helpful? Give feedback.
-
For anyone having the issue a possible solution, we had for testing STAGE and VITE_STATE environment variables set, they need to be the same as the NODE_ENV (in our case) so I needed to set NODE_ENV also to development in our local hot reload E2E test setup. So my theory is, that somewhere a development build has expected but the NODE_ENV var produced a production build which was causing the issue. Maybe that might help anyone. |
Beta Was this translation helpful? Give feedback.
-
I'm stuck and would appreciate some help resolving a production build problem with a
ViteJS React Typescript
project I'm porting from CRA to ViteJS. I cannot find any posts on the topic of how to debug this situation.My project runs perfectly with
yarn dev
, however it crashes in the browser when serving the production buildyarn build && yarn serve
and there is so little information available that I cannot tack the origin of the problem. The error is:react_devtools_backend.js:2540 Error: Minified React error #130; visit https://reactjs.org/docs/error-decoder.html?invariant=130&args[]=object&args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
After some searching, I found a recommendation to change the
vite.config.ts
to help debug what is failing in a build situation. The recommendvite.config.ts
is:However, when attempting a build and serve this configuration, I get the following error. I have verified a boilerplate ViteJS starter project also produces this error with the above configuration.
Uncaught TypeError: jsxDevRuntime.exports.jsxDEV is not a function at index.e025178a.js:1045 (anonymous) @ index.e025178a.js:1045
Upon inspection of the generated code running in the browser, the
ReactDOM.render
call is transformed tojsxDevRuntime.exports.jsxDEV
whereasjsxDEV
isundefined
.This is where I am stuck and would appreciate some help.
Beta Was this translation helpful? Give feedback.
All reactions