Search code examples
aws-lambdaaws-cdkknex.jsesbuildaws-cdk-typescript

When synth or deploy with AWS CDK getting bundling error related to knex


I am using only the mssql client. But I am getting below bundling errors when I try to run cdk synth.

  • Node.js - v18.16.0
  • CDK - 2.95.1
  • knex - 2.5.1
dirname /.../my-repo/cdk/my-app/lib/lambda
Bundling asset api---app/get-last-sales-no/Code/Stage...
✘ [ERROR] Could not resolve "sqlite3"

    node_modules/knex/lib/dialects/sqlite3/index.js:42:19:
      42 │     return require('sqlite3');
         ╵                    ~~~~~~~~~

  You can mark the path "sqlite3" 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 "pg"

    node_modules/knex/lib/dialects/pgnative/index.js:13:19:
      13 │     return require('pg').native;
         ╵                    ~~~~

  You can mark the path "pg" 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 "mysql2"

    node_modules/knex/lib/dialects/mysql2/index.js:15:19:
      15 │     return require('mysql2');
         ╵                    ~~~~~~~~

  You can mark the path "mysql2" 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 "pg"

    node_modules/knex/lib/dialects/redshift/index.js:44:19:
      44 │     return require('pg');
         ╵                    ~~~~

  You can mark the path "pg" 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.

Did some research and found out that some got this fixed by marking knex itself or the client dependencies as external for esbuild. The thing is I am not using esbuild directly. AWS CDK should be using it under the hood I guess.

Anyway to fix this?


Solution

  • I was able to resolve this by defining external modules for esbuild bundler used under the hood by aws-cdk-lib/aws-lambda-nodejs

    const lambdaFunction = new NodejsFunction(instance, 'get-last-sales-no', {
        bundling: {
          externalModules: [
            'sqlite3',
            'mysql',
            'oracledb',
            'mysql2',
            'pg',
            'pg-query-stream',
          ],
        },
        ...
    });
    

    Ref: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_nodejs-readme.html#externals