thgh / vercel-sapper

Vercel builder for Sapper with SSR enabled
MIT License
191 stars 25 forks source link

Using `node/fs` causes serverless issues #23

Closed tetigi closed 4 years ago

tetigi commented 4 years ago

Hello,

now-sapper has been working great, but I recently added a couple of extra modules to my package.json, and now I'm getting the following error when Now tries to upload my serverless function:

Error: The Serverless Function "index" is 58.69mb which exceeds the maximum size limit of 50mb.

Unfortunately, it's a little challenging to debug as all of this is happening on Zeit-side, so I can't poke around (to my knowledge) and see why the index.js is so big.

Do you have any insight on why the function is so large? Is it pulling in all of the node modules?

Context: The specific module is node, which I'm using for node/fs on the backend - trying to access some markdown files located in the src tree.

thgh commented 4 years ago

Don't install the node package. It's built in: import * as fs from 'fs'

tetigi commented 4 years ago

Thanks for the quick response!

I think the original title is a red-herring then - it seems that the symptom is 'using fs causes now-sapper issues'. I made the smallest possible demo project to show this (based on the my-sapper-app demo from Zeit):

https://github.com/tetigi/my-sapper-app

This injects a file read from ./test.txt into the blog slugs. It works totally fine locally with, or without importing node - however, when I try to run this on Now, index starts throwing errors and the app refuses to load.

Reading from fs here: https://github.com/tetigi/my-sapper-app/blob/master/src/routes/blog/%5Bslug%5D.json.js

Here's the generated page: https://my-sapper-app-git-master.tetigi.now.sh/

Here's the function logs:

[GET] /
06:43:12:43
Status:-1
Duration:56.2ms
Memory Used:24 MB
ID:vgf49-1585204992432-e4f3e5dc251b
User Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:74.0) Gecko/20100101 Firefox/74.0
RequestId: 5635161d-4a59-45ee-a7d1-9cd2ec8b45a0 Error: Runtime exited with error: exit status 129Runtime.ExitError

Here's the build logs: https://pastebin.com/jKyAyFG8

Are there any other ways I can debug this? So far I've kinda hit a brick wall.

thgh commented 4 years ago

Looks like the function exits sooner than it reads the file which lambda does not seem to like. How about waiting for the file to be read?

For serverless to work properly, you should write your code as if every invocation starts from scratch. You can cache values, but you shouldn't try to run async stuff (reading files) after your function has completed.

https://github.com/tetigi/my-sapper-app/pull/1/files

Also related: only static and __sapper__ are copied into the function so your file is probably missing which would throw an exception.

tetigi commented 4 years ago

Ack on the static above - I was just about to say! Looks like this fixes it - I'm new to serverless, so also thank you for the tips. Coming from server-land, it's hard when you can't just ssh onto the box and see what's going on!