Skip to content

Serverless-webpack with NestJS throwing error with dependency injection, not injecting classes #692

@thebrianbug

Description

@thebrianbug

This is a Bug Report

Hello there! I'm running into an issue with DI when deploying to serverless using serverless-webpack. The service runs locally 100% fine, and also deploys via webpack without an error, but when I try to hit the default /api route with a GET, I get an internal server error. Logs show that the service that is supposed to be instantiated via Nest DI is not and is instead undefined. Here's a repo with the issue. See the README for steps.

I have a repo that reproduces my issue. See the README for an explanation of the steps I took to make the repo.

Description

For bug reports:

  • What went wrong? Error in sever response with GET /api after a sls deploy. Running locally via npm start sls-nest returns no error.
    {
        "statusCode": 500,
        "message": "Internal server error"
    }
  • What did you expect should have happened? GET /api should not return an error
    {
      "message": "Welcome to sls-nest!"
    }
  • What was the config you used?
    const path = require('path');
    const slsWebpack = require('serverless-webpack');
    const nodeExternals = require('webpack-node-externals');
    const tsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
    
    module.exports = {
      externals: [nodeExternals()],
      devtool: 'source-map',
      entry: slsWebpack.lib.entries,
      resolve: {
        extensions: ['.js', '.jsx', '.json', '.ts', '.tsx'],
        plugins: [new tsconfigPathsPlugin({ configFile: 'tsconfig.base.json' })],
      },
      target: 'node',
      module: {
        rules: [
          {
            test: /\.(ts|js)x?$/,
            exclude: /node_modules/,
            use: [
              {
                loader: 'babel-loader',
              },
            ],
          },
        ],
      },
      plugins: [],
      mode: slsWebpack.lib.webpack.isLocal ? 'development' : 'production',
      stats: 'minimal', // errors-only, minimal, none, normal, verbose
      output: {
        libraryTarget: 'commonjs',
        path: path.resolve(__dirname, '.webpack'),
        filename: '[name].js',
      },
      optimization: {
        minimize: false,
      },
    };
  • What stacktrace or error message from your provider did you see?
    {
        "level": 50,
        "time": 1613515874136,
        "pid": 8,
        "hostname": "169.254.197.221",
        "req": {
            "id": 1,
            "method": "GET",
            "url": "/api",
            "headers": {
                "accept": "*/*",
                "accept-encoding": "gzip, deflate, br",
                "cloudfront-forwarded-proto": "https",
                "cloudfront-is-desktop-viewer": "true",
                "cloudfront-is-mobile-viewer": "false",
                "cloudfront-is-smarttv-viewer": "false",
                "cloudfront-is-tablet-viewer": "false",
                "cloudfront-viewer-country": "US",
                "host": "3l7q3apkvd.execute-api.us-east-2.amazonaws.com",
                "postman-token": "8490d083-c684-42f7-9a91-fad5d572795d",
                "user-agent": "PostmanRuntime/7.26.8",
                "via": "1.1 891b1deb65550a4f9d40f2585712c586.cloudfront.net (CloudFront)",
                "x-amz-cf-id": "YO-Up50TAIIkKVjg-QzmDh4pzx4pisdU90aryCxoh-0jLn6KFjGuMA==",
                "x-amzn-trace-id": "Root=1-602c4c61-7075cde85ba389fb39b638bb",
                "x-forwarded-for": "107.141.127.45, 70.132.9.83",
                "x-forwarded-port": "443",
                "x-forwarded-proto": "https",
                "connection": "close"
            }
        },
        "context": "ExceptionsHandler",
        "trace": "TypeError: Cannot read property 'getData' of undefined\n    at AppController.getData (/var/task/apps/sls-nest/src/handler.js:179:28)\n    at /var/task/node_modules/@nestjs/core/router/router-execution-context.js:38:29\n    at InterceptorsConsumer.intercept (/var/task/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:11:20)\n    at /var/task/node_modules/@nestjs/core/router/router-execution-context.js:46:60\n    at /var/task/node_modules/@nestjs/core/router/router-proxy.js:9:23\n    at Layer.handle [as handle_request] (/var/task/node_modules/express/lib/router/layer.js:95:5)\n    at next (/var/task/node_modules/express/lib/router/route.js:137:13)\n    at Route.dispatch (/var/task/node_modules/express/lib/router/route.js:112:3)\n    at Layer.handle [as handle_request] (/var/task/node_modules/express/lib/router/layer.js:95:5)\n    at /var/task/node_modules/express/lib/router/index.js:281:22",
        "msg": "Cannot read property 'getData' of undefined"
    }

For feature proposals:

  • What is the use case that should be solved. The more detail you describe this in the easier it is to understand for us.
    • I'm trying to use serverless-webpack within a nx monorepo.
  • If there is additional config how would it look

Similar or dependent issue(s):

Additional Data

  • Serverless-Webpack Version you're using: "serverless-webpack": "^5.3.5",
  • Webpack version you're using: I'm not sure, see the repo at top
  • Serverless Framework Version you're using: "serverless": "^2.25.1",
  • Operating System: OS X 11.1
  • Stack Trace (if available):

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions