3

I have an issue when I deploy our next js app to AWS amplify the build done but CustomerError occurs that blocks my deployment: here are my logs

2024-01-25T20:11:31.852Z [INFO]: 
2024-01-25T20:11:31.857Z [INFO]: Done in 175.65s.
2024-01-25T20:11:31.874Z [INFO]: # Completed phase: build
2024-01-25T20:11:31.894Z [INFO]: ## Build completed successfully
2024-01-25T20:11:31.896Z [INFO]: # Starting caching...
2024-01-25T20:11:31.909Z [INFO]: # Creating cache artifact...
2024-01-25T20:13:18.327Z [INFO]: # Created cache artifact
2024-01-25T20:13:18.480Z [INFO]: # Uploading cache artifact...
2024-01-25T20:13:28.603Z [INFO]: # Uploaded cache artifact
2024-01-25T20:13:28.703Z [INFO]: # Caching completed
2024-01-25T20:13:28.715Z [ERROR]: !!! CustomerError: Can't find required-server-files.json in build output directory
2024-01-25T20:13:28.716Z [INFO]: # Starting environment caching...
2024-01-25T20:13:28.716Z [INFO]: # Environment caching completed
Terminating logging...
2
  • 2
    Got the same at the end of successful build. I use Next.js v14 with { output: "export" } config I run on custom build image with Node 20: public.ecr.aws/docker/library/node:20.11.0 Commented Jan 27, 2024 at 19:48
  • Same on my side with Amplify, still cannot find a solution Commented Jan 29, 2024 at 11:32

6 Answers 6

2

Hi the main issue maybe because the AWS Amplify is not recognising that your app is NextJS - SSR or NextJS - SSG.

So to approach this, you can use the AWS CLI to solve this, remember to create an IAM account with the correct provision rules to work with Amplifier

  1. Run the following AWS CLI commands
aws amplify update-app --app-id <APP_ID> --platform WEB --region <REGION>
aws amplify update-branch --app-id <APP_ID> --branch-name <BRANCH_NAME> --framework 'Next.js - SSG' --region <REGION>
  1. Update your build spec to point the baseDirectory to out. e.g.
version: 1
frontend:
  ...
  artifacts:
    baseDirectory: out
  ...
  1. Trigger rebuild through commit, or manually

  2. You ready to go

Sometime the URL by Amplify, for example the built one is mysite.com, but when you clicked it appear nothing, actually because the actual routes is mysite.com/index.html

So if you want to set up the URL, You can go to the AWS Amplify Hosting console, go under All app / <Your Repo Name> / Hosting, and go to the Rewrites and redirects tab.

Then for example, if you want:

mysite.com/index.html -> mysite.com

You can set:

Source URL Target URL Type Country Code
/index.html / 200 (Rewrite) <Can be blank>

If you have some other routes, like /example.html but you dont want the .html ending, you can do this

mysite.com/example.html -> mysite.com/example

Source URL Target URL Type Country Code
/<any>.html /<any> 200(Rewrite) <Can be blank>

Thats my initial approach, hope that it is useful. Peace!

Sign up to request clarification or add additional context in comments.

3 Comments

this is the way
I got it working with this approach, using the aws cli to force it to change to SSG. Additionally, I needed assetPrefix: '/' inside next.config.js and delete all redirect/rewrite rules in side hosting page in cosole.
I wanted to add, I spent forever trying to get it working with manual zip upload, it should work for SSG and it does not. I had to connect the repo, select the branch, and let it detect my app. Then switch SSR to SSG after deployment, and redeploy again
2

with next 14.2 on amplify we had the same error initially.

  • removed output: 'export', from next.config.mjs
  • make sure package.json build script is just "build": "next build",
  • amplify build script baseDirectory: .next

Comments

1

Ran into the same problem using nextjs with a SSG app. what fixed it for me was changing the build script to "build": "next build && next export" which causes amplify to identify the app as SSG. and then i added the actual build script (since next export is deprecated) "actualBuild": "next build" and ran the actual build script.

1 Comment

After a frustrating 2 days, the above solution is what worked. Effectively the suggestion here is to "trick" AWS Amplify into understanding that the app is an SSG / SSR ( because that's where the issue seems to be w.r.t required-server-files.json ) . For my NextJS ( 14 + ) I pointed my build command to "next build" and then had a dfferent "actualBuild" command which had the real commands that needed to be run. It finally worked. I moved from having 2/3 build fails due to missing required-server-files.json to consistent builds.
0

in amplify.yml it is because of artifacts: baseDirectory: "path_is_wrong"

base directory will start from appRoot that you provided in amplify.yml and since it is next base directory's last folder should be something like "dist/apps/my-app/.next"

5 Comments

BuildSpec: | version: 1.0 frontend: phases: preBuild: commands: - nvm use 18 - yarn install build: commands: - env | grep -e NEXT_PUBLIC_ >> .env.production - yarn run build artifacts: baseDirectory: .next files: - "*/" cache: paths: - node_modules/**/* here i add the path of my build directory its look good but still get same error
im not an expert but can you try to change the files: - '/' to files: -'**/' and/or baseDirectory: ./.next
BuildSpec: | version: 1.0 frontend: phases: preBuild: commands: - nvm use 18 - yarn install build: commands: - env | grep -e NEXT_PUBLIC_ >> .env.production - yarn run build artifacts: baseDirectory: ./.next files: - “*/” cache: paths: - node_modules/**/* Yeah I change this path but still got the same issue
ibb.co/8j4LdvQ here you can see my complete BuildSpec steps is there anything that I missed?
that looks good to me. yarn run build locally and check if .next folder is really in root of project if it is there sorry pal i rlly don't know
0

If you use Next.js with { output: "export" } config. Add 'next export" in package.json

// package.json
...
"scripts": {
  "dev": "next dev",
  "build": "next build && next export",  // Add next export
  "start": "next start",
  "lint": "next lint"
},
...

Amplify performs an automatic build based on the "scripts" in package.json.

next export is deprecated and replaced with "output": "export" since v13.3. However, However, amplify still decides how to build through "next export".

Comments

0

After some trail and error this worked for me:

version: 1
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - npm run build
  artifacts:
    baseDirectory: .next
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

1 Comment

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.