jetbridge / cdk-nextjs

Deploy a NextJS application using AWS CDK
https://constructs.dev/packages/cdk-nextjs-standalone
Apache License 2.0
251 stars 38 forks source link

Cannot find package 'next' imported from /var/task/index.mjs" #169

Open sonnydg opened 8 months ago

sonnydg commented 8 months ago

Hello! I am having the following issue when accessing the Cloudfront URL:

I see this error through CloudWatch in the nextjsServerFn lambda Cannot find package 'next' imported from /var/task/index.mjs"

cdk-nextjs v4.0.0-beta.10 - next@13

bestickley commented 8 months ago

Hi @sonnydg, can you provide a minimal reproducible repo?

sonnydg commented 7 months ago

@bestickley use this example: https://github.com/jetbridge/cdk-nextjs/tree/main/examples/app-router

What I see is that deploying cdk in Windows gives those errors that I understand may be due to the folders and path, in Ubuntu doing a deployment with cdk works without problem.

bestickley commented 7 months ago

@sonnydg, thanks for the context. Could https://github.com/jetbridge/cdk-nextjs/issues/166 be the root issue?

sonnydg commented 7 months ago

Not in this case, that error (which I solved in my locale) means that you cannot do the deploy, in this case it is already deployed and when viewing the domain it shows the error: Cannot find package 'next' imported from /var/task/index.mjs"

The way I could solve it is, this line and archive https://github.com/jetbridge/cdk-nextjs/blob/main/src/NextjsServer.ts#L117

lambda.Code.fromAsset(this.props.nextBuild.nextServerFnDir)

This would solve the error previously, from what I understand is that the problem comes with the bucket to deploy the asset.

then there is another error that is from nextJS Error: Cannot find module 'next/dist\client\components\static-generation-async-storage.external.js'

here is issue in next: https://github.com/vercel/next.js/issues/58244

and from here I moved to Linux to be able to move forward more fluidly.

bestickley commented 7 months ago

Are you recommending a change from code: Code.fromBucket(asset.bucket, asset.s3ObjectKey), to code: Code.fromAsset(this.props.nextBuild.nextServerFnDir) ? Is this a windows specific issue?

sonnydg commented 7 months ago

@bestickley

Are you recommending a change from code: Code.fromBucket(asset.bucket, asset.s3ObjectKey), to code: Code.fromAsset(this.props.nextBuild.nextServerFnDir) ? Is this a windows specific issue?

I recommend using formAsset because the CDK is compatible with any system that is deployed, it is responsible for packaging and uploading by cloudformation in the lambda that the source needs, this is done "natively" so I understand that it is not applied this way by some reason and I would need to understand why it is with fromBucket. Looking at the code a little, I understand that it is like this to be able to replace some parameters in the code from lambda and then upload the source to the lambda server.

Why is it necessary to perform such action having environment variable by lambda and aws SDK? I don't know.

bestickley commented 7 months ago

@sonnydg, I understand your confusion. I wish we could use fromAsset. We cannot in order to support PNPM monorepos that use symlinks. Please see: https://github.com/jetbridge/cdk-nextjs/blob/main/docs/code-deployment-flow.md#pnpm-monorepo-symlinks

sonnydg commented 7 months ago

@bestickley I understand that I think there is already a solution for this. https://github.com/aws/aws-cdk/pull/18216 (add doc monorepo)

https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_nodejs.NodejsFunctionProps.html#depslockfilepath For cases of isolating or containerizing, it should be a standard to use CDK for lambda and

https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_nodejs.NodejsFunctionProps.html#projectroot This will be useful in the case of monorepo, I understand that the package must be specified in a root to apply to the different libraries within that folder (according to the doc mentioned above) in this case nextjs.

const server = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'server', {
  entry: "../server/src/index.ts",    
  projectRoot: "../server",
  depsLockFilePath: "../server/package-lock.json",
  runtime: cdk.aws_lambda.Runtime.NODEJS_14_X,
});
bestickley commented 7 months ago

@sonnydg, I tried to get this to work with native CDK for 2 weeks and could not. If you can get it to work that would be awesome. I would happily accept a PR.

sonnydg commented 7 months ago

@bestickley I ask you, did you try using outputFileTracingRoot from the following configuration? It is precisely to use monorepo.

https://nextjs.org/docs/pages/api-reference/next-config-js/output#caveats

bestickley commented 7 months ago

@sonnydg, I did not, but Next.js build related functionality is the responsibility of open-next in this construct's current setup. If you can get it to work leveraging that setting, please let me know.

sonnydg commented 7 months ago

@sonnydg, I did not, but Next.js build related functionality is the responsibility of open-next in this construct's current setup. If you can get it to work leveraging that setting, please let me know.

What open-next does applies to the project in Next, so this configuration is valid to carry out without problems. I used outputFileTracingRoot incorrectly and when deploying the URL it did not work it gave an error that it does not find the libraries, this tells me that it is possible to generate monorepo using that parameter in next-config. With this parameter you can specify a ROOT of the libraries to be used as a monorepo. I'm not 100% aware but I'm sure it will work if you look for more information on this. I can't take care of doing tests due to lack of time, but maybe at some point in the future I will dedicate myself to trying to solve it.

bestickley commented 7 months ago

@sonnydg, thank you for looking into this. When you have time, please create a PR so I can review and test out.

bestickley commented 7 months ago

@sonnydg, I think I know understand the issue you're having and have found a solution. I'll track my work at https://github.com/jetbridge/cdk-nextjs/issues/183. It will take me a couple weeks to create a PR but please be on the lookout.

sonnydg commented 6 months ago

@bestickley ooh! makes sense, it's good that you were able to find the problem. Thank you!

bestickley commented 6 months ago

Hey @sonnydg, could you try out the branch of this PR to see if it resolves your issue? https://github.com/jetbridge/cdk-nextjs/pull/192

sonnydg commented 6 months ago

@bestickley hello! Sorry for the delay in response.

I'm going to try to test it in a new environment since I migrated everything to Linux :D, give me some time and I'll do the test.

bestickley commented 5 months ago

@sonnydg, have you been able to test within windows on new PR?

sonnydg commented 5 months ago

Sorry, I haven't been able to do the tests yet because I've migrated everything to Linux, I don't have Windows :(

Maybe someone else can do the test.