Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda fails with "Cannot find module..."

I'm packing my lambdas with Webpack. And I'm deploying them with CDK.

However, when testing them through ApiGateway, I get this:

Lambda execution failed with status 200 due to customer function error: Error: Cannot find module 'apollo-server-lambda'.

Here's my Webpack config:

{
  mode: 'production',
  entry: {
    graphql: './src/lambdas/graphql.ts',
    cognito: './src/lambdas/cognito.ts',
    'smoke-detector': './src/lambdas/smoke-detector.ts',
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.resolve(__dirname, "dist"),
    filename: '[name]/[name].js'
  },
  target: 'node',
  externals: [nodeExternals()],
  resolve: {
    extensions: ['.ts', '.js']
  },
  module: {
    rules: [
      loader({
        test: /\.ts$/,
        use: {
          loader: 'ts-loader',
          options: {
            getCustomTransformers: () => ({
              before: [graphqlTag.getTransformer()]
            })
          }
        }
      }),
      loader({
        test: /\.graphqls$/,
        use: {
          loader: 'graphql-tag/loader',
        }
      })
    ]
  },
  plugins: [
  ]
}

Executing the output file like node graphql.js doesn't fail. What am I doing wrong?

like image 591
Daniel Birowsky Popeski Avatar asked Mar 08 '26 16:03

Daniel Birowsky Popeski


1 Answers

Removing

externals: [nodeExternals()]

fixed the problem.

It caused dependencies to be excluded from the bundle.

like image 118
Daniel Birowsky Popeski Avatar answered Mar 11 '26 09:03

Daniel Birowsky Popeski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!