serverless-bundle icon indicating copy to clipboard operation
serverless-bundle copied to clipboard

serverless bundle breaks nestjs configuration

Open softmarshmallow opened this issue 5 years ago • 5 comments

create boiler plate nestjs project. from serverless-examples/with-typescript-nestjs

run it as is -> works well

remove all plugins and replace it with serverless-bundle

Not that it does not compiles, it throws wierd error.

[ExceptionHandler] Nest cannot create the module instance. Often, this is because of a circular dependency between modules. Use forwardRef() to avoid it.

(Read more: https://docs.nestjs.com/fundamentals/circular-dependency)
Scope []
   +294ms
Error: Nest cannot create the module instance. Often, this is because of a circular dependency between modules. Use forwardRef() to avoid it.

(Read more: https://docs.nestjs.com/fundamentals/circular-dependency)
Scope []
  
    at NestContainer.addModule (/Users//Documents/GitHub/services/g11n-service/.webpack/service/src/webpack:/Users//Documents/GitHub/services/node_modules/@nestjs/core/injector/container.js:45:1)
    at DependenciesScanner.insertModule (/Users//Documents/GitHub/services/g11n-service/.webpack/service/src/webpack:/Users//Documents/GitHub/services/node_modules/@nestjs/core/scanner.js:61:1)
    at DependenciesScanner.scanForModules (/Users//Documents/GitHub/services/g11n-service/.webpack/service/src/webpack:/Users//Documents/GitHub/services/node_modules/@nestjs/core/scanner.js:31:1)
    at DependenciesScanner.scan (/Users//Documents/GitHub/services/g11n-service/.webpack/service/src/webpack:/Users//Documents/GitHub/services/node_modules/@nestjs/core/scanner.js:25:1)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at /Users//Documents/GitHub/services/g11n-service/.webpack/service/src/webpack:/Users//Documents/GitHub/services/node_modules/@nestjs/core/nest-factory.js:89:1
    at Function.asyncRun (/Users//Documents/GitHub/services/g11n-service/.webpack/service/src/webpack:/Users//Documents/GitHub/services/node_modules/@nestjs/core/errors/exceptions-zone.js:18:1)
    at NestFactoryStatic.initialize (/Users//Documents/GitHub/services/g11n-service/.webpack/service/src/webpack:/Users//Documents/GitHub/services/node_modules/@nestjs/core/nest-factory.js:88:1)
    at NestFactoryStatic.create (/Users//Documents/GitHub/services/g11n-service/.webpack/service/src/webpack:/Users//Documents/GitHub/services/node_modules/@nestjs/core/nest-factory.js:34:1)
 ✘@-MacBook-Pro  ~/Documents/GitHub/services/g11n-service   service/g11n ●  (node:56887) UnhandledPromiseRejectionWarning: RpcIpcMessagePortClosedError: Cannot send the message - the message port has been closed for the process 56887.
    at /Users//Documents/GitHub/services/node_modules/serverless-bundle/node_modules/fork-ts-checker-webpack-plugin/lib/rpc/rpc-ipc/RpcIpcMessagePort.js:47:47
    at processTicksAndRejections (internal/process/task_queues.js:83:21)
(node:56887) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:56887) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

softmarshmallow avatar Dec 10 '20 05:12 softmarshmallow

I've heard of some other folks having issues with Nestjs. Do you know if we can use Webpack with it?

jayair avatar Jan 03 '21 02:01 jayair

'Cause there are lots of examples using nestjs with webback on nestjs's official example and serverless-examples.

It's odd that it breaks. (on run time.)

softmarshmallow avatar Jan 03 '21 06:01 softmarshmallow

@jayair it's pretty easy to reproduce. are you planning to support nestjs?

softmarshmallow avatar Mar 07 '21 09:03 softmarshmallow

Hey, I achieved to make a nestjs app working with serverless with this config:

package:
  individually: true
custom:
  bundle:
    sourcemaps: true
    tsConfig: 'tsconfig.app.json'
    linting: true
    packager: yarn
    forceInclude:
      - '@slynova/flydrive-s3'
    ignorePackages:
      - pg-native
      - nats
      - mqtt
      - kafkajs
      - grpc
      - apollo-server-express
      - apollo-server-fastify
      - class-transformer/storage
      - cache-manager
      - '@nestjs/websockets/socket-module'
    forceExclude:
      - aws-sdk
    externals:
      - argon2 # https://github.com/serverless-heaven/serverless-webpack/issues/659

It's working on local invoke but failed on AWS Lambda, no error, just some weird timeout... Try to increase the timeout (to 2min) but still stuck during the bootstrap of nestjs.

I think it's because it bundles it bit too much, Nestjs recommand to not bundle in one file but only compile the code & create a minimal package.json with dependencies. Seems a good way to work with layer, add all dependencies to a layer & compile only the lambda code.

Happy to talk with someone who struggles on this to find a solution and share it with others :ok_hand:

EDIT: Found a solution, it works with this config, just need to exclude all modules with native bindings (such as pg-native). The bundle can only bundle JavaScript modules but some modules requires native bindings. For this specific native modules I recommend using the plugin serverless-layers plugin the generate a layer with these modules and put them as externals.

NestJs community doesn't recommend to bundle node_modules with NestJs apps see this repository -> https://github.com/ZenSoftware/bundled-nest

The problem here is if you don't bundle with optimization you will have a huge node_modules (for my app it's around 256Mo of node_modules).

There are solutions to optimize your package for example using .yarnclean (if using yarn), excluding not used or already in layers dependencies. Here is an article about this -> https://itnext.io/3x-smaller-lambda-artifacts-by-removing-junk-from-node-modules-2b50780ca1f5

I recommend using webpack-bundle-analyzer to analyze your bundle and find any optimization you can make on your bundle. I think there is an issue on this repo to include it as paramater #227 , But you only have to set stats: true which will generate the json file which can be analyse by webpack-bundle-analyser.

webpack-bundle-analyzer bundle/output/path/stats.json

Feel free to complete any tips here to bundle NestJs Lambda!

Nightbr avatar May 19 '21 15:05 Nightbr

@Nightbr i have issue like this:

✖ Cannot read properties of undefined (reading 'name') ✖ TypeError: Cannot read properties of undefined (reading 'name') at PartialType (/Users/project/.webpack/service/dist/webpack:/@nestjs/mapped-types/dist/partial-type.helper.js:19:26) at Module.../../../dist/post/dto/update-post.dto.js (/Users/project/.webpack/service/dist/webpack:/src/post/dto/update-post.dto.ts:4:35) at __webpack_require__ (/Users/project/.webpack/service/dist/webpack:/webpack/bootstrap:19:1) at Module.../../../dist/post/post.controller.js (/Users/project/.webpack/service/dist/webpack:/src/post/post.controller.ts:13:1) at __webpack_require__ (/Users/project/.webpack/service/dist/webpack:/webpack/bootstrap:19:1) at Module.../../../dist/post/post.module.js (/Users/project/.webpack/service/dist/webpack:/src/post/post.module.ts:3:1) at __webpack_require__ (/Users/project/.webpack/service/dist/webpack:/webpack/bootstrap:19:1) at Module.../../../dist/app.module.js (/Users/project/.webpack/service/dist/webpack:/app.module.ts:6:1) at __webpack_require__ (/Users/project/.webpack/service/dist/webpack:/webpack/bootstrap:19:1) at /Users/project/.webpack/service/dist/webpack:/main.ts:2:1 at /Users/project/.webpack/service/dist/main.js:128404:3 at Object.<anonymous> (/Users/project/.webpack/service/dist/main.js:128409:12) at Module._compile (node:internal/modules/cjs/loader:1103:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) at /Users/project/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:157:133 at processTicksAndRejections (node:internal/process/task_queues:96:5) at InProcessRunner.run (/Users/project/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:157:9)

pashatw avatar Apr 27 '22 04:04 pashatw