Sometimes i get `Error: Cannot find module 'prepar...
# help
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: {
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
Hey @Sahan Amadoruge, let me loop in @thdxr, he’s more familiar with this.
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
@Mischa Spiegelmock I'm also this getting error on my deployed lambdas
Are you guys using any layers or monitoring solution with your functions?
I’m using a layer for prisma
if i remove the layer I get the same error
in local debug mode does this happen after a stack deploy?
or completely randomly
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
Can I see how you're defining your function?
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
I'm curious why your project is set to esm? I use
statements everywhere but I just let esbuild transpile all the way down
Also what does
resolve to
@thdxr I'm getting this randomly in local environment.
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
package inside a monorepo
if I move SST to the top level I get this error again
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
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:
the other issues I'm working around now with this:
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
and it's not an issue at all. I also have a
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 
 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
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
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
Yeah I saw that PR, I'm merging in a pretty massive refactor this week that should make a lot of that easier
this is what my TS monorepo setup with typescript project references and npm workspaces looks like
configuring jest is the most annoying part but the rest is ez
@thdxr any idea on the original issue i had? 🙄
sorry to hijack your thread sahan
@Sahan Amadoruge can you verify that it happens even without an infrastructure deploy?
It's strange. Now i'm not getting it. I've deployed to a whole new stage