Hi, I’m trying to use Knex with SST. It works when...
# help
g
Hi, I’m trying to use Knex with SST. It works when I run it locally with
sst start
, but when I run
sst deploy
I get the following error:
Copy code
2021-06-17T19:18:15.874Z	undefined	ERROR	Uncaught Exception 	{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module '../../dialects/postgres/index.js'\nRequire stack:\n- /var/task/invoke.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module '../../dialects/postgres/index.js'",
        "Require stack:",
        "- /var/task/invoke.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:999:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
        "    at Module.load (internal/modules/cjs/loader.js:863:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
        "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
        "    at internal/main/run_main_module.js:17:47"
    ]
}
It’s odd because I can see
knex/lib/dialects/postgres/index.js
in my node modules folder. I also have pg installed. I have tried explicitly importing pg in my lambda but to no avail. Here’s the lambda code:
Copy code
import Knex from "knex";
const pgEnv = process.env.POSTGRES || "";
const config = {
  client: "pg",
  connection: pgEnv,
};
const knex = Knex(config);
I found a webpack workaround for this error is to add the following to the `webpack.config.js`:
Copy code
module.exports = {
  entry: slsw.lib.entries,
  target: "node",
  externals: { knex: "commonjs knex" },
};
Is there a similar workaround that would be compatible with SST? Thanks!
r
I think you should be able to label the knex dependency as external using this: https://docs.serverless-stack.com/constructs/Function#externalmodules
g
Thanks! Have marked it as an external module, but now running into this
Error: Cannot find module 'knex'
Sorry if I’m missing something glaringly obvious.
r
Can you share the code you're using to specify it?
t
You probably want the nodeModules option
Not external
Mixed this same thing up yesterday
r
Looks like you're right, I was thinking of the serverless-esbuild plugin that installs the dependency from node_modules if it's set in externals and referenced in code. It seems here you have to separately specify externalModuels and/or nodeModules
g
yup I marked both knex and pg as nodeModules and now it’s running fine - thanks!
c
It's been 5 months but I just want to thank you guys for this thread as its just solved an issue I've been having for hours, Thanks!