cdk-nextjs: `required-server-files.json: No such file or directory`

I’m encountering an error in the beforeBundling hook of the ImageOptimizationLambda class. The CDK app for my project is located in a directory named cdk. The Next app is located in a directory named web-frontend. These two directories both exist directly below the project root directory, which is named next-lambda.

.
├── cdk
│   ├── bin
│   │   └── cdk.ts
│   ├── lib
│   │   └── web-app-stack.ts
│   ├── cdk.json
│   └── package.json
├── web-frontend
│   ├── .next
│   ├── next.config.js
│   ├── ...
│   ├── ...
│   └── package.json
// cdk/lib/web-app-stack.ts

import { Construct } from 'constructs';
import { Stack, StackProps } from 'aws-cdk-lib';
import { Nextjs } from 'cdk-nextjs-standalone';

export class WebAppStack extends Stack {
    constructor(scope: Construct, id: string, props?: StackProps) {
        super(scope, id, props);

        new Nextjs(this, 'Web', {
            nextjsPath: '../web-frontend'
        });
    }
}

When I execute cdk deploy, the asset bundling stage fails with the error cp: cannot stat ‘/Users/Alistair/Sites/next-lambda/web-frontend/.next/web-frontend/.next/required-server-files.json’: No such file or directory. The actual locations of required-server-files.json are web-frontend/.next and web-frontend/.next/standalone/.next.

I’ve tried moving the Next application into the cdk directory and changing the value of the nextjsPath prop to ./web-frontend. Using this directory structure, the error reads cp: cannot stat ‘/Users/Alistair/Sites/next-lambda/cdk/web-frontend/.next/standalone/web-frontend/.next/required-server-files.json’: No such file or directory. In this case, the required-server-files.json does exist at the location specified in the error message, but seemingly could not be found when the code ran.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 16 (9 by maintainers)

Most upvoted comments

@lukeshay what does your file structure look like?

root
  -> apps
    -> infra (contains cdk)
    -> website (contains nextjs app)

This is how I have the Nextjs construct configured:

const nextjs = new Nextjs(this, "Website", {
  buildPath: resolve("../.."),
  nextjsPath: resolve("../website"),
});

I have to run cdk synth from apps/infra for it to work properly.

I am hitting this as well in a yarn monorepo that is using turbo. The path to required-server-files.json has <app-dir>/.next twice. Using resolve for the nextjsPath fixed it for me.

@alistairdivorty which version are you using? @khuezy could be wrong output tracing root