sst / open-next

Open source Next.js serverless adapter
https://open-next.js.org
MIT License
3.7k stars 111 forks source link

Serverless CLI: Image Optimization Lambda seems to try using inexistent __dirname,"./threadChild.js" #373

Closed celso-alexandre closed 4 months ago

celso-alexandre commented 4 months ago

open-next version: 2.3.6 System: Linux Mint Linux 5.15.0-88-generic #98-Ubuntu SMP Mon Oct 2 15:18:56 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Deploy: I'm using serverless CLI. I may be missing something, maybe SST would've add the missing file before zipping it

before:package:createDeploymentArtifacts:
  - yarn open-next build --minify
  - mkdir -p ./.open-next/zips
  - cd .open-next/server-function && zip -r ../zips/server-function.zip .
  - cd .open-next/image-optimization-function && zip -r ../zips/image-optimization-function.zip .
  - cd .open-next/revalidation-function && zip -r ../zips/revalidation-function.zip .
  - cd .open-next/warmer-function && zip -r ../zips/warmer-function.zip .

My full serverless.yml: https://github.com/sst/open-next/issues/32#issuecomment-1973330303

Description: It seems that the generated "image-optimization" lambda is trying to import the file __dirname,"./threadChild.js", which was not generated

This is the cloudwatch log error I'm getting:

ERROR   Uncaught Exception  
{
    "errorType": "Error",
    "errorMessage": "Cannot find module '/var/task/threadChild.js'",
    "code": "MODULE_NOT_FOUND",
    "requireStack": [],
    "stack": [
        "Error: Cannot find module '/var/task/threadChild.js'",
        "    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)",
        "    at Module._load (node:internal/modules/cjs/loader:985:27)",
        "    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)",
        "    at MessagePort.<anonymous> (node:internal/main/worker_thread:186:26)",
        "    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:826:20)",
        "    at exports.emitMessage (node:internal/per_context/messageport:23:28)"
    ]
}

The following were produced by yarn open-next build open-next version: 2.3.6 image image

conico974 commented 4 months ago

2.3.6 is broken with image opt, you can downgrade to 2.3.5 or set SHARP_VERSION="0.32.6" env variable before running open-next build. Someone has reported an issue with 0.32.6 as well but i haven't had time to reproduce the issue yet and it seems that most people are managing to make it work with 0.32.6. That's the PR for 2.3.6, there is more info here : https://github.com/sst/open-next/pull/361

celso-alexandre commented 4 months ago

Thanks for replying, and I must say I'm very much amused by this piece of software, and I've learned a lot from it. I've updated the description with my current serverless.yml definition.

I should add that after downgrading sharp to 0.32.6 and setting the variable as building SHARP_VERSION="0.32.6" yarn open-next build --minify, the lambda function "image-optimization-function" still produces an import of the inexistent file "./threadChild.js". I will soon report back if the error persists

khuezy commented 4 months ago

What next version are you currently on? sharp said https://github.com/vercel/next.js/releases/tag/v14.1.1-canary.28 fixed some things regards to it; can you try installing the latest 14.1.1 canary to see if it does indeed fix the problem?

celso-alexandre commented 4 months ago

Thanks for helping. I can confirm after a couple tests that this exact line is not called anymore by changing the sharp version to 0.32.6. Many thanks