AnomalyInnovations / serverless-bundle

Optimized packages for ES6 and TypeScript Node.js Lambda functions without any configuration.
https://serverless-stack.com/chapters/package-lambdas-with-serverless-bundle.html
MIT License
536 stars 157 forks source link

Lambda Logs: Error: Cannot find module 'knex' #246

Closed remcoabalain closed 3 years ago

remcoabalain commented 3 years ago

Hi, we've implemented knex with knex-aurora-data-api-client. I noticed in the docs that the module knex is excluded from the bundle, but I didn't see how knex would be available at the server on runtime. And indeed this looks like to be a problem on runtime, our functions fail because of the module "knex" cannot be found. I've been searching all day, but can't find a solution. I noticed some other Knex related issues, but none of them fixes our problem. The project is build via seed.run. Any ideas to solve our problem? Thanks in advance 😄

Environment:

  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              12.19.1
     Framework Version:         2.46.0 (local)
     Plugin Version:            5.4.0
     SDK Version:               4.2.3
     Components Version:        3.12.0

Dependencies

"devDependencies": {
    "@babel/cli": "^7.14.5",
    "@babel/core": "^7.14.6",
    "@babel/preset-typescript": "^7.14.5",
    "@tsconfig/node12": "^1.0.9",
    "@types/node": "^15.12.5",
    "@types/uuid": "^8.3.0",
    "aws-sdk": "^2.936.0",
    "babel-eslint": "^10.1.0",
    "babel-plugin-module-resolver": "^4.1.0",
    "serverless-bundle": "^5.0.0-beta.2",
    "serverless-dotenv-plugin": "^3.9.0",
    "serverless-offline": "^7.0.0",
    "ts-loader": "^9.2.3"
  },
  "dependencies": {
    "@babel/polyfill": "^7.12.1",
    "@jumpventures/stack": "^0.70.9",
    "@slack/web-api": "^6.2.4",
    "data-api-client": "^1.2.0",
    "knex": "^0.95.6",
    "knex-aurora-data-api-client": "^1.5.5",
    "object-mapper": "^6.2.0",
    "short-uuid": "^4.2.0",
    "slugify": "^1.5.3",
    "sqlstring": "^2.3.2",
    "uuid": "^8.3.2",
    "validate.js": "^0.13.1"
  }
remcoabalain commented 3 years ago

Full error:

{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'knex'\nRequire stack:\n- /var/task/functions/organization.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'knex'",
        "Require stack:",
        "- /var/task/functions/organization.js",
        "- /var/runtime/UserFunction.js",
        "- /var/runtime/index.js",
        "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:1068:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)",
        "    at Module.load (internal/modules/cjs/loader.js:933:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:774:14)",
        "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)",
        "    at internal/main/run_main_module.js:17:47"
    ]
}
remcoabalain commented 3 years ago

Never mind! We've found it. We needed to add knex-aurora-data-api-client to the forceInclude/external config as well 😅 . Since that package requires knex as well, we shoudn't let webpack build it inside the package.

custom:
  bundle:
    disableForkTsChecker: true
    forceInclude:
      - mysql
      - knex-aurora-data-api-client
    externals:
      - knex-aurora-data-api-client