Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NestJS: vite command not working #38

Closed
Floppy012 opened this issue Mar 23, 2022 · 3 comments
Closed

NestJS: vite command not working #38

Floppy012 opened this issue Mar 23, 2022 · 3 comments

Comments

@Floppy012
Copy link

Running vite with NestJS fails with the following error (build is working):

Pre-bundling dependencies:
  @nestjs/core
  @nestjs/common                                                                                                                                                                                                                              
(this will be run only when your dependencies or config have changed)
✘ [ERROR] Could not resolve "@nestjs/microservices"

    node_modules/@nestjs/core/nest-factory.js:52:128:
      52 │         const { NestMicroservice } = (0, load_package_util_1.loadPackage)('@nestjs/microservices', 'NestFactory', () => require('@nestjs/microservices'));
         ╵                                                                                                                                 ~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path "@nestjs/microservices" as external to exclude it from the bundle, which
  will remove this error. You can also surround this "require" call with a try/catch block to handle
  this failure at run-time instead of bundle-time.

✘ [ERROR] Could not resolve "cache-manager"

    node_modules/@nestjs/common/cache/cache.providers.js:16:116:
      16 │             const cacheManager = (0, load_package_util_1.loadPackage)('cache-manager', 'CacheModule', () => require('cache-manager'));
         ╵                                                                                                                     ~~~~~~~~~~~~~~~

  You can mark the path "cache-manager" as external to exclude it from the bundle, which will remove
  this error. You can also surround this "require" call with a try/catch block to handle this
  failure at run-time instead of bundle-time.

✘ [ERROR] Could not resolve "class-validator"

    node_modules/@nestjs/common/pipes/validation.pipe.js:31:182:
      31 │         return (validatorPackage !== null && validatorPackage !== void 0 ? validatorPackage : (0, load_package_util_1.loadPackage)('class-validator', 'ValidationPipe', () => require('class-validator')));
         ╵                                                                                                                                                                                       ~~~~~~~~~~~~~~~~~

  You can mark the path "class-validator" as external to exclude it from the bundle, which will
  remove this error. You can also surround this "require" call with a try/catch block to handle this
  failure at run-time instead of bundle-time.

✘ [ERROR] Could not resolve "class-transformer"

    node_modules/@nestjs/common/serializer/class-serializer.interceptor.js:22:252:
      22 │ ...ltOptions === void 0 ? void 0 : defaultOptions.transformerPackage) !== null && _a !== void 0 ? _a : (0, load_package_util_1.loadPackage)('class-transformer', 'ClassSerializerInterceptor', () => require('class-transformer'));
         ╵                                                                                                                                                                                                              ~~~~~~~~~~~~~~~~~~~

  You can mark the path "class-transformer" as external to exclude it from the bundle, which will
  remove this error. You can also surround this "require" call with a try/catch block to handle this
  failure at run-time instead of bundle-time.

✘ [ERROR] Could not resolve "class-transformer"

    node_modules/@nestjs/common/pipes/validation.pipe.js:34:190:
      34 │         return (transformerPackage !== null && transformerPackage !== void 0 ? transformerPackage : (0, load_package_util_1.loadPackage)('class-transformer', 'ValidationPipe', () => require('class-transformer')));
         ╵                                                                                                                                                                                               ~~~~~~~~~~~~~~~~~~~

  You can mark the path "class-transformer" as external to exclude it from the bundle, which will
  remove this error. You can also surround this "require" call with a try/catch block to handle this
  failure at run-time instead of bundle-time.

✘ [ERROR] Could not resolve "@nestjs/websockets/socket-module"

    node_modules/@nestjs/core/nest-application.js:18:115:
      18 │ const { SocketModule } = (0, optional_require_1.optionalRequire)('@nestjs/websockets/socket-module', () => require('@nestjs/websockets/socket-module'));
         ╵                                                                                                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path "@nestjs/websockets/socket-module" as external to exclude it from the
  bundle, which will remove this error. You can also surround this "require" call with a try/catch
  block to handle this failure at run-time instead of bundle-time.

✘ [ERROR] Could not resolve "@nestjs/microservices/microservices-module"

    node_modules/@nestjs/core/nest-application.js:19:132:
      19 │ const { MicroservicesModule } = (0, optional_require_1.optionalRequire)('@nestjs/microservices/microservices-module', () => require('@nestjs/microservices/microservices-module'));
         ╵                                                                                                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path "@nestjs/microservices/microservices-module" as external to exclude it from
  the bundle, which will remove this error. You can also surround this "require" call with a
  try/catch block to handle this failure at run-time instead of bundle-time.

✘ [ERROR] Could not resolve "@nestjs/microservices"

    node_modules/@nestjs/core/nest-application.js:117:128:
      117 │         const { NestMicroservice } = (0, load_package_util_1.loadPackage)('@nestjs/microservices', 'NestFactory', () => require('@nestjs/microservices'));
          ╵                                                                                                                                 ~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path "@nestjs/microservices" as external to exclude it from the bundle, which
  will remove this error. You can also surround this "require" call with a try/catch block to handle
  this failure at run-time instead of bundle-time.
@GauBen
Copy link

GauBen commented Mar 27, 2022

Hey there, you'll need to add theses lines in vite.config.js:

export default defineConfig({
  // ...
  optimizeDeps: {
    // Vite does not work well with optionnal dependencies,
    // mark them as ignored for now
    exclude: [
      '@nestjs/microservices',
      '@nestjs/websockets',
      'cache-manager',
      'class-transformer',
      'class-validator',
      'fastify-swagger',
    ],
  },
})

@angelhdzdev
Copy link

Hey there, you'll need to add theses lines in vite.config.js:

export default defineConfig({
  // ...
  optimizeDeps: {
    // Vite does not work well with optionnal dependencies,
    // mark them as ignored for now
    exclude: [
      '@nestjs/microservices',
      '@nestjs/websockets',
      'cache-manager',
      'class-transformer',
      'class-validator',
      'fastify-swagger',
    ],
  },
})

This solved my error, thanks!

@axe-me
Copy link
Owner

axe-me commented May 11, 2022

yea this is a limitation of vitejs atm, hopefully it get improved in upcoming vite 3
vitejs/vite#6007

@axe-me axe-me closed this as completed May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants