Have you ever wondered how easy it can be to deploy and host an API? Scalable, stable, a piece of cake to deploy and costs almost nothing. The goal...
For further actions, you may consider blocking this person and/or reporting abuse
Thanks for this tutorial. Sadly after running sls deploy, when I go to the production URL ending with / song, I get a 500 error. In Cloudwatch the error is: Cannot find module './dist/lambda.js'". Any idea on how to fix it? Cheers
It might be because
nest-cli.json
config file only expectsmain.ts
to get compiled tomain.js
. So, one should specifylambda.ts
as well or instead writehanlder()
function inmain.ts
.I tried both options, but both results were Cannot find module either ./dist/lambda.js or ./dist/main.js
For the first: I added the entryFile to my out-of-the-box best v.10.0.0 different looking nest-cli.json :
For the second option, I added the handler to the main.ts
and updated the serverless.yml from
to
my tsconfig.json in case is relevant is:
Thanks for the reply! I guess I have to try the guide my own to check the reason solid clear. 'll get back with an update!
I'm back! :)
I've tried the whole thing with my NestJS + serverless.yaml setup and everything works good enough. However, I'm using Yandex Serverless Functions instead of AWS Lambda, but I tend to think this part is irrelevant to the issue.
Here're some of the info:
9.5.0
A note here! In the
package
rule I'm making sure that the dist directory with mymain.ts
file is compiled and other files that I do need.I'm not entirely sure what the exact reason behind your issue, but I guess you could try to debug if the main.ts/lambda.ts is being compiled into main.js/lambda.js and, furthermore, being packaged by Serverless Framework (in a .zip archive). I hope my configs will be of some help
Just by adding the package section to the serverless.yml, and changing the handler there to dist/src/main.handler. Make it work, except now it's complaining about not finding modules, so I need to install them one by one : tslib,uid, iterare .... and it does not sound good. What am I missing?
OK. Now I would say some files are not packaged at all. Could you check the packaged file system in AWS Lambda editor? It seems the package.json is missing
The file is present in the zip I downloaded from the lambda console.
Could you please show the latest error you've got?
ERROR Error: Cannot find module 'fast-safe-stringify'
Require stack:
I have uploaded my code to: github.com/arcovoltaico/nextjs
I remember a similar error in my setup. I had a module that wasn't compatible in this serverless build shema. I would try to build a serverless function with a minimum number of dependencies to figure out is it a general issue for all of your dependencies or a particular one.
At least, this is what I did with my codename.
Thanks for the git link. I've checked some of the configs, so a few notes:
serverless.yml
should bedist/main.handler
, notdist/src/main.handler
. It's the path your nest app compiles things to.main.ts
with the code inlamda.ts
, since you're running a cachedServer inlamda.ts
I think that was not the case. I compared this project with a plain Severless one that also works after being deployed, and I realised that the node-modules folder did contain all the child dependencies because I used npm.
So the culprit is pnpm, and it happens to NOT install them. I'm still not understanding how it can work locally. The fix is instead of running
npmp install
, choosepnpm install --shamefully-hoist
instead.Maybe not ideal, but the only option I know so far.
I'm glad your issue got resolved! Although, I'm quite sure with point 2 mentioned just above.
p.s. You could also try to switch to the basic npm as an ubiquitous standard and swap it with pnpm when there is a need for some optimization.
Happy serverless coding anyway!
Interesting and detailed discussion, thanks for your comments! I'm glad that you managed to resolve it in the end! I wasn't able to pitch in unfortunately.
Thanks, great article.
Earlier I was able to run and test nest-api locally by running npm run start:dev.
But running this cmd with serverless-express , its not running on locally with watch mode.
How can I do this , so that both npm run start:dev gives me local api to play around.
Hi, thanks for reading and for your comment! I believe you should be able to run it just as before, so npm run start:dev should work. The serverless-express wrapper is only ran when it's deployed via serverless.
To emulate Lambda locally serverless-offline plugin can be used, however I am not sure if it supports the watch mode equivalent.
In any case let me know if you find any specifics, or if I misunderstood your question.
Thanks, I was following another tut also, got confused. In another tut it modify the main.ts rather than creating a new file lambda.ts.
Cool 😉
Hi, thanks for the article!
I was just wondering if you've run into the issue where DTO decorators for
class-transformer
andclass-validator
work locally in testing, but do not work once deployed to AWS ApiGateway/Lambda? Locally, the request will just be outright reject and it won't even attempt to process the request, when deployed it will attempt to process a request with a malformed DTO.I am using the exact same tooling as you, and very similar set up.
Thanks!
Hi, thanks for the comment. If I understand your question correctly, I'd say that this is expected behavior. Lambda should proxy every request, it's up to your Nest app to handle validation responses, and just proxy them back to API Gateway.
If I misunderstood then feel free to provide more details about your issue and I'll try to pitch in. 👍