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

Cannot build with Webpack #486

Closed
2 of 4 tasks
hjf opened this issue Oct 1, 2021 · 5 comments
Closed
2 of 4 tasks

Cannot build with Webpack #486

hjf opened this issue Oct 1, 2021 · 5 comments

Comments

@hjf
Copy link

hjf commented Oct 1, 2021

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When running with nest start, it works as expected. When running with build --webpack, it fails because it cannot find some path. This seems to have been removed by upstream (typestack/class-transformer#563)

Implemented the proposed solution in the link

Minimum reproduction code

https://github.com/hjf/mapped-types-bug

Steps to reproduce

  1. yarn
  2. yarn build --webpack
  3. see error

Expected behavior

It should build with no errors

Package version

1.0.0

Node.js version

14.17.4

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@micalevisk
Copy link
Member

For folks looking this, the error is the following:

ERROR in ./node_modules/@nestjs/mapped-types/dist/type-helpers.utils.js 69:27-63
Module not found: Error: Can't resolve 'class-transformer/storage' in '/tmp/mapped-types-bug/node_modules/@nestjs/mapped-types/dist'
resolve 'class-transformer/storage' in '/tmp/mapped-types-bug/node_modules/@nestjs/mapped-types/dist'
  Parsed request is a module
  using description file: /tmp/mapped-types-bug/node_modules/@nestjs/mapped-types/package.json (relative path: ./dist)
    resolve as module
      /tmp/mapped-types-bug/node_modules/@nestjs/mapped-types/dist/node_modules doesn't exist or is not a directory
      /tmp/mapped-types-bug/node_modules/@nestjs/mapped-types/node_modules doesn't exist or is not a directory
      /tmp/mapped-types-bug/node_modules/@nestjs/node_modules doesn't exist or is not a directory
      /tmp/mapped-types-bug/node_modules/node_modules doesn't exist or is not a directory
      looking for modules in /tmp/mapped-types-bug/node_modules
        /tmp/mapped-types-bug/node_modules/class-transformer doesn't exist
      /tmp/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory
 @ ./node_modules/@nestjs/mapped-types/dist/index.js 19:27-58
 @ ./node_modules/@nestjs/mapped-types/index.js 6:9-26
 @ ./node_modules/@nestjs/swagger/dist/type-helpers/intersection-type.helper.js 4:23-54
 @ ./node_modules/@nestjs/swagger/dist/type-helpers/index.js 13:13-50
 @ ./node_modules/@nestjs/swagger/dist/index.js 18:13-38
 @ ./node_modules/@nestjs/swagger/index.js 6:9-26
 @ ./src/app.controller.ts 13:16-42
 @ ./src/app.module.ts 11:25-52
 @ ./src/main.ts 4:21-44

@kamilmysliwiec
Copy link
Member

Add class-transformer/storage to lazyImports https://github.com/hjf/mapped-types-bug/blob/main/webpack.config.js#L2

@adworacz
Copy link

adworacz commented Dec 7, 2021

I spent a few hours on this particular issue, especially when using fastify as well.

This feels like a potential gap in the existing NestJS serverless documentation. Would a pull request be accepted to update the documentation with the following example?

For those still wrestling with this issue, my webpack.config.js (targeting AWS Lambda) looked like so:

const path = require('path')

// Documentation:
// https://docs.nestjs.com/faq/serverless#serverless
// https://github.com/nestjs/swagger/issues/1334#issuecomment-836488125

// Tell webpack to ignore specific imports that aren't
// used by our Lambda but imported by NestJS (can cause packing errors).
const lazyImports = [
  '@nestjs/microservices/microservices-module',
  '@nestjs/websockets/socket-module',
  '@nestjs/platform-express',
  'swagger-ui-express',
  'class-transformer/storage' // https://github.com/nestjs/mapped-types/issues/486#issuecomment-932715880
]

module.exports = (options, webpack) => ({
  ...options,
  mode: 'production',
  target: 'node14',
  entry: {
    index: './lib/lambda.js',
  },
  output: {
    filename: '[name].js',
    libraryTarget: 'umd',
    path: path.join(process.cwd(), 'build/dist'),
  },
  externals: {
    'aws-sdk': 'aws-sdk',
  },
  plugins: [
    ...options.plugins,
    new webpack.IgnorePlugin({
      checkResource(resource) {
        if (lazyImports.includes(resource)) {
          try {
            require.resolve(resource)
          } catch (err) {
            return true
          }
        }
        return false
      },
    }),
  ],
})

@max5432112345

This comment was marked as off-topic.

@elucidsoft

This comment was marked as off-topic.

@nestjs nestjs locked and limited conversation to collaborators Jun 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants