How to fix the "regeneratorRuntime is not defined" error in Vite + Nuxt 3?

Почему при перезагрузке страницы в режиме desktop возникает ошибка "regeneratorRuntime is not defined", в то время как в мобильном режиме она отсутствует?
  • 9 декабря 2023 г. 12:16
Ответы на вопрос 1
"regeneratorRuntime is not defined" error typically occurs when your code uses async/await syntax but doesn't properly include the Babel plugin for regenerator runtime. To fix this error in Vite + Nuxt 3, you can follow these steps:

1. Install additional dependencies:
   ```bash
   npm install --save-dev @babel/plugin-transform-runtime
   npm install --save @babel/runtime
   ```

2. Create a `babel.config.js` file in the root of your project if it doesn't exist, and add the following configuration:
   ```javascript
   module.exports = {
     presets: [
       [
         '@babel/preset-env',
         {
           targets: {
             node: 'current',
           },
         },
       ],
     ],
     plugins: [['@babel/plugin-transform-runtime']],
   };
   ```

3. In your Nuxt 3 configuration file (`nuxt.config.js`), make sure to add the following configuration:
   ```javascript
   export default {
     // ...
     build: {
       babel: {
         plugins: [['@babel/plugin-transform-runtime']],
       },
     },
   };
   ```

4. Restart your Vite + Nuxt 3 development server.

This configuration ensures that the Babel plugin for regenerator runtime is properly included, allowing your async/await syntax to work correctly.

Regarding the error appearing only in the desktop mode and not in the mobile mode, it could be due to different transpilation or build setups for different targets. Double-check your build configurations and ensure that the mentioned babel runtime plugin is included consistently across all builds.
Похожие вопросы