Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sonnydg opened this issue Nov 13, 2023 · 20 comments
Closed

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

sonnydg opened this issue Nov 13, 2023 · 20 comments

Comments

@sonnydg
Copy link

sonnydg commented Nov 13, 2023

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
Copy link
Contributor

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

@sonnydg
Copy link
Author

sonnydg commented Nov 13, 2023

@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
Copy link
Contributor

@sonnydg, thanks for the context. Could #166 be the root issue?

@sonnydg
Copy link
Author

sonnydg commented Nov 14, 2023

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:
vercel/next.js#58244

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

@bestickley
Copy link
Contributor

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
Copy link
Author

sonnydg commented Nov 14, 2023

@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
Copy link
Contributor

bestickley commented Nov 14, 2023

@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
Copy link
Author

sonnydg commented Nov 14, 2023

@bestickley
I understand that I think there is already a solution for this.
aws/aws-cdk#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
Copy link
Contributor

@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
Copy link
Author

sonnydg commented Nov 14, 2023

@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
Copy link
Contributor

@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
Copy link
Author

sonnydg commented Nov 14, 2023

@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
Copy link
Contributor

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

@bestickley
Copy link
Contributor

bestickley commented Dec 11, 2023

@sonnydg, I think I know understand the issue you're having and have found a solution. I'll track my work at #183. It will take me a couple weeks to create a PR but please be on the lookout.

@sonnydg
Copy link
Author

sonnydg commented Dec 15, 2023

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

@bestickley
Copy link
Contributor

Hey @sonnydg, could you try out the branch of this PR to see if it resolves your issue?
#192

@sonnydg
Copy link
Author

sonnydg commented Jan 2, 2024

@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
Copy link
Contributor

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

@sonnydg
Copy link
Author

sonnydg commented Feb 10, 2024

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.

@bestickley
Copy link
Contributor

closing in favor of #183

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants