Amplify references the env file(.env.production) from an incorrect path when running Next.js app standalone build.

0

Next.js Version

14.2.25 (page routing, standalone build)

Project Type

monorepo

In this case, let's assume the app root is admin-front/front

Issue

When Amplify runs Next.js, it references the env file ($appRoot/.env.production) instead of the one in the build output folder (.next/.../.env.production).

Expected Behavior

The env file should be referenced from the build output folder (.next/.../.env.production).

Steps to Reproduce

  1. Amplify runs npm run build 👍

  2. The build output folder is created as .next 👍

  3. The file admin-front/front/.next/standalone/admin-front/front/.env.production is automatically generated with the same content as admin-front/front/.env.production 👍

  4. As instructed in amplify.yml under build.commands, the contents of admin-front/front/.next/standalone/admin-front/front/.env.production are modified (because I want to change the contents after build) 💡

  5. Amplify starts the Next.js application. 👍

  6. However, instead of using admin-front/front/.next/standalone/admin-front/front/.env.production, Next.js references admin-front/front/.env.production, causing the modified values to be ignored. 🥲

In standalone mode, Amplify should not reference files from the source root. This issue does not occur in other development environments (computers).

asked 2 months ago32 views
1 Answer
-1

This issue you're experiencing with AWS Amplify and Next.js is indeed unexpected behavior. In a standalone build of a Next.js application, the environment variables should be read from the build output folder, not from the source root.

For Next.js applications in standalone mode, the build process typically generates a standalone folder within the .next directory. This folder contains a complete, self-contained version of your application, including all necessary files and configurations. The .env.production file in this standalone folder should be the one used when running the application, not the one in the source root.

The fact that Amplify is referencing the .env.production file from the incorrect path (the source root instead of the build output folder) suggests there might be an issue with how Amplify is configured to run your Next.js application in standalone mode.

To address this, you could try the following:

  1. Review your amplify.yml configuration file. Ensure that you're explicitly specifying the path to the standalone build output when starting your Next.js application.

  2. You might need to adjust your build commands or artifacts configuration in the amplify.yml file to ensure that Amplify is using the correct directory structure for your standalone build.

  3. Consider adding a step in your build process that copies the modified .env.production file to the correct location in the standalone build folder, overwriting the automatically generated one.

  4. If the issue persists, you may need to reach out to AWS support or check the Amplify GitHub issues for any known problems related to standalone Next.js builds.

Remember, when using monorepo structures or non-standard project layouts with Amplify, extra care may be needed to ensure all paths and configurations are correctly specified.
Sources
Build container ran out of memory | AWS re:Post
AWS Amplify Resources - Amazon Web Services

profile picture
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions