sladg / nextjs-lambda

Lambda deployments for Nextjs12 & Nextjs13 (standalone). Easy CLI commands to get your standalone Next output to run in AWS Lambda (not @Edge)! Uses CDK in behind and produces code zips importable to Terraform, Serverless, Azure, etc.
MIT License
169 stars 19 forks source link

Deploy via CDK requires extra dependencies #96

Closed jlong-icon closed 10 months ago

jlong-icon commented 11 months ago

Hi,

I am trying to follow the first option here: https://github.com/sladg/nextjs-lambda/blob/master/docs/CDK.md. I am finding the my cdk deploy fails because of missing dependencies. Namely: next, react, react-dom, and config.json.

I think it has to do with this line. To test this, I removed the chunk of code from dist/index.js that starts with // lib/server-handler/index.ts and I am able to get past these dependencies.

Thanks

sladg commented 11 months ago

Hey! Can you share some sample app? This is meant specifically for NextJS. Aka. not having next as dependency is not really an option :)

jlong-icon commented 11 months ago

Hi, thanks for the response.

I have recreated the problem here: https://github.com/jlong-icon/next-lambda-cdk-issue-demo. Here is how. you can recreate the issue (from my repo):

  1. In my-app, run yarn, yarn build, npx @sladg/nextjs-lamabda pack
  2. In cdk, run yarn, yarn build, yarn cdk deploy.

You should see the following error:

Error: Cannot find module 'next/dist/server/next-server'
Require stack:
- /Users/jameslong/git/next-lambda-cdk-issue-demo/cdk/node_modules/@sladg/nextjs-lambda/dist/index.js

You can add the error dependencies if you want, but eventually it'll error because it is missing config.json as there is no way around this (except modifying dist/index.js like I said above).

Let me know if you have nay problems or trouble recreating my steps.

sladg commented 11 months ago

You are running npx @sladg/nextjs-lamabda pack inside my-app, however, according to error, it resolves to cdk folder.

/Users/jameslong/git/next-lambda-cdk-issue-demo/cdk/node_modules/@sladg/nextjs-lambda/dist/index.js

Considering the route, you are not running npx from global cache, but from locally installed instance. Remove it from dependencies everywhere and try again :)

EDIT: I would suggest creating single package.json and include CDK as well as Next app there.

jlong-icon commented 11 months ago

Hi, thanks for taking the time to respond. Sorry I have been slow to respond, I have been on vacation.

I am not sure if I understand your last comment. My issue is not with pack (in my-app/). Rather, my issue is with yarn cdk deploy (from the cdk/) directory. And I do need @sladg/nextjs-lambda in cdk/ because I am referencing it in lib/cdk-stack.ts (as are you on line 3 of your example here).

I'm reluctant to try your suggestion in the edit. I'd prefer to keep my deployment separate from my app. And the only way I can think to truly have a single package.json is to run create-next-app and cdk init in the same directory, which may introduce other conflicts.

sladg commented 10 months ago

This is project-specific issue with setup. I did test and all seems fine, your dependency tree is wrong.