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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deploy via CDK requires extra dependencies

jlong-icon opened this issue · comments

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

commented

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

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.

commented

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.

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.

commented

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