Sometimes i get `Error: Cannot find module 'prepar...
# help
s
Sometimes i get
Error: Cannot find module 'prepareUpload'
error. When i trigger the below lambda function
Copy code
function: {
  handler: "src/upload/prepareUpload.main",
  environment: {
    ...props.dbEnvs,
  },
},
Here's the full error
Copy code
Error: Cannot find module 'prepareUpload'
Require stack:
- /Users/jsdev/Desktop/Workspace/test_sst/node_modules/@serverless-stack/aws-lambda-ric/lib/utils/UserFunction.js
- /Users/jsdev/Desktop/Workspace/test_sst/node_modules/@serverless-stack/aws-lambda-ric/lib/index.js
- /Users/jsdev/Desktop/Workspace/test_sst/node_modules/@serverless-stack/aws-lambda-ric/bin/index.js
f
Hey @Sahan Amadoruge, let me loop in @thdxr, he’s more familiar with this.
m
I’m getting
Cannot find module 'preSignUp' Require stack: - /var/runtime/UserFunction.js - /var/runtime/index.js.
types of errors when I try to run any of my deployed lambdas
s
@Mischa Spiegelmock I'm also this getting error on my deployed lambdas
t
Are you guys using any layers or monitoring solution with your functions?
m
I’m using a layer for prisma
if i remove the layer I get the same error
t
in local debug mode does this happen after a stack deploy?
or completely randomly
m
i have only tried deploying my stack so far, have never tried local dev yet
any idea what the error means? this is kinda blocking my transition to SST
t
Can I see how you're defining your function?
m
it happens for all of them but it goes something like this
my project is ESM btw but I believe esbuild is transpiling it to CJS, dunno if that makes any difference
t
I'm curious why your project is set to esm? I use
import
statements everywhere but I just let esbuild transpile all the way down
Also what does
getProjectRoot()
resolve to
s
@thdxr I'm getting this randomly in local environment.
m
getProjectRoot just returns an absolute path to the root of my project. Function bundles my functions fine, i see the output as i posted in that screenshot
I am using ESM because it’s the standard and because it’s needed for tree shaking to work with esbuild. i need tree shaking to try to keep lambda cold start times down a bit, because the more deps you have the more code it has to load at startup.
any suggestions of things I can try changing here to try to deal with this error?
maybe the absolute path is part of the problem. if I download the lambda package I see:
if I make it a relative path then it starts having trouble building files though:
my CDK stuff is in an
infra
package inside a monorepo
if I move SST to the top level I get this error again
t
Your package doesn't need to be esm for tree shaking to work
Only dependencies of your package need to be ESM
Right now sst needs to be run from the root, sst.json should be at the root of your repo
m
what if I have a large project split into multiple packages in a monorepo?
it's still an issue to run the SST CJS build scripts inside my ESM project though without this patch: https://github.com/serverless-stack/serverless-stack/pull/1112
t
m
the other issues I'm working around now with this: https://github.com/serverless-stack/serverless-stack/issues/1113
hm.
Packages need to be built before they can be imported, which means if working across multiple packages you'll need a complex TypeScript watcher setup.
This doesn't require a complex setup, I'm using project references and
tsc --build
with
--watch
and it's not an issue at all. I also have a
tsconfig.base.json
file which all the sub-projects inherit. the sub-projects just specify which other packages they depend on. This makes
More configuration to manage. You'll need a 
tsconfig.json
 per package to get things working exactly right.
somewhat (not totally but mostly) a moot point. I also have my frontend and a chrome extension in the same project and those should be separate packages. I'm using npm v7 workspaces so no need for yarn
t
I do want to support esm properly, I started going down that route a few months ago but decided a larger refactor was needed first
Yeah I still keep my frontend as a separate package
m
just for lols I converted cli, core, and resources to ESM over the weekend. I don't recommend merging it but it's all there if you want to take a look
but i don't know how to convert it to ESM and have it still work with CJS projects 😞
very depressing
t
Yeah I saw that PR, I'm merging in a pretty massive refactor this week that should make a lot of that easier
m
awesome!
this is what my TS monorepo setup with typescript project references and npm workspaces looks like https://github.com/jetbridge/typescript-cdk-template
configuring jest is the most annoying part but the rest is ez
s
@thdxr any idea on the original issue i had? 🙄
m
sorry to hijack your thread sahan
t
@Sahan Amadoruge can you verify that it happens even without an infrastructure deploy?
s
It's strange. Now i'm not getting it. I've deployed to a whole new stage