Hello all, I'm getting a `ERROR Error: Cannot find...
# help
e
Hello all, I'm getting a
ERROR Error: Cannot find module '/Users/erik/src/dataworks/dwam-back/.build/src/../config/config.js'
at runtime in local debug mode when calling the /list lambda endpoint where I'm adding Sequelize. My project structure is like this : /sst.json /package.json /stacks /src/list.js my lambda endpoint, which does an import { db } from '../models'; /models/index.js which does a const config = require(__dirname + '/../config/config.js')[env]; /config/config.js I see two possible issues : 1. In my .build folder there is a src subfolder but no models or config subfolders 2. the error above comes from .build/src/list.js where models/index.js has been included into this flattened version of list.js but in that context the dynamic require could be pointing to a different folder than intended when authoring it (in my case it's ok since src+models+config are under the root / Any help would be welcome. It might be bad JS usage of my end of import/require since I'm not yet fully comfortable with those and the implications, but I've tried other variants without success + it wouldn't address issue #1 AFAIK.
t
Is there a reason you're using dirname instead of directly requiring it? Since it's dynamic esbuild cannot bundle config.js which is why it's missing
You can require relatively from the current file without that
e
well for that case I can do as you suggest (and it works), but right after in the same /models/index.js does a dynamic loading of all other .js files in that models folder. How would I be able to handle that ?
t
Are you sure you need to dynamically load things? That's generally not common. If you do really need to do that you can use https://docs.serverless-stack.com/constructs/Function#copyfiles to copy additional files into your bundle
e
ok, thanks for the pointer, that's what I thought was the right direction as well. I'll think about it see if I can avoid that. I guess I could manually list all the files to load... not convenient but it would do the job.... In the end does one option vs the other (no dynamism vs dynamism with copyFiles) make any difference ?
t
Maybe a slighter cold start increase with the dynamic approach since it has to load them but probably not super significant
esbuild probably has a plugin that allows you to do wildcard imports from a folder at compile time
e
ok, thanks a lot for all the pointers I'll look into it if need be. I'm amazed as to how reactive you are and knowledgeable. It's people like you that make products super attractive as we (developer users) have trust in the support we can get around it. Kudos.
t
Of course! Thanks for trying out sst 👍🏽