{"id":811,"hash":"38fcfdda35d85b9afbbc7be90ad4b1f3b48ae78b2ecc7556385c4c0fb9ab3304","pattern":"When synth or deploy with AWS CDK getting bundling error related to knex","full_message":"I am using only the mssql client. But I am getting below bundling errors when I try to run cdk synth.\n\nNode.js - v18.16.0\nCDK - 2.95.1\nknex - 2.5.1\n\ndirname /.../my-repo/cdk/my-app/lib/lambda\nBundling asset api---app/get-last-sales-no/Code/Stage...\n✘ [ERROR] Could not resolve \"sqlite3\"\n\n    node_modules/knex/lib/dialects/sqlite3/index.js:42:19:\n      42 │     return require('sqlite3');\n         ╵                    ~~~~~~~~~\n\n  You can mark the path \"sqlite3\" as external to exclude it from the bundle, which will remove this\n  error. You can also surround this \"require\" call with a try/catch block to handle this failure at\n  run-time instead of bundle-time.\n\n✘ [ERROR] Could not resolve \"pg\"\n\n    node_modules/knex/lib/dialects/pgnative/index.js:13:19:\n      13 │     return require('pg').native;\n         ╵                    ~~~~\n\n  You can mark the path \"pg\" as external to exclude it from the bundle, which will remove this\n  error. You can also surround this \"require\" call with a try/catch block to handle this failure at\n  run-time instead of bundle-time.\n\n✘ [ERROR] Could not resolve \"mysql2\"\n\n    node_modules/knex/lib/dialects/mysql2/index.js:15:19:\n      15 │     return require('mysql2');\n         ╵                    ~~~~~~~~\n\n  You can mark the path \"mysql2\" as external to exclude it from the bundle, which will remove this\n  error. You can also surround this \"require\" call with a try/catch block to handle this failure at\n  run-time instead of bundle-time.\n\n✘ [ERROR] Could not resolve \"pg\"\n\n    node_modules/knex/lib/dialects/redshift/index.js:44:19:\n      44 │     return require('pg');\n         ╵                    ~~~~\n\n  You can mark the path \"pg\" as external to exclude it from the bundle, which will remove this\n  error. You can also surround this \"require\" call with a try/catch block to handle this failure at\n  run-time instead of bundle-time.\n\nDid 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.\n\nAnyway to fix this?","ecosystem":"npm","package_name":"aws-lambda","package_version":null,"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\n\nconst lambdaFunction = new NodejsFunction(instance, 'get-last-sales-no', {\n    bundling: {\n      externalModules: [\n        'sqlite3',\n        'mysql',\n        'oracledb',\n        'mysql2',\n        'pg',\n        'pg-query-stream',\n      ],\n    },\n    ...\n});\n\nRef: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_nodejs-readme.html#externals","confidence":0.7000000000000001,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/77131329/when-synth-or-deploy-with-aws-cdk-getting-bundling-error-related-to-knex","votes":3,"created_at":"2026-04-19T04:51:44.616694+00:00","updated_at":"2026-04-19T04:51:44.616694+00:00"}