'Next.js standalone not reading env file

I have a question about my Next.js project.

I have configured my project to be build over the standalone mode for the deployment

experimental: {
    outputStandalone: true
}

Using this, it generates me a standalone folder with a server.js like expected.

The main issue is that I am using an env variable in my sources, called NEXT_PUBLIC_API_BASE_URL

When I start my project in development mode (using next serve), it works fine.

But when I start the standalone generated file (using node server.js) it does not work.

It seems that the file is loaded on the "server side", when I console.log its value in the .next/standalone/server/pages/_app.js it shows the right value in the node console.

But it looks like next is using files under .next/static/chunks/pages/ and an other app.js that does not seem to access process.env (on browser side).

I thought that prefixing my env var with NEXT_PUBLIC was meant to work this way, it seems not.

Any idea on how it works there ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source