How to read .env in production ? (Docker & pm2)
Hey all,
I'm trying to deploy a Nuxt app in production and need to host it somewhere where I'll not need to worry about execution time - so I opted for a vps.
Premise:
I have a typed environment that checks process.env when starting (it's imported in the nuxt.config file). So to build I have to pass the env variables to it.
However, when I try to start the node server from the .output folder, the env variables are again undefined and the .env file appears to not be read correctly. How can I have it read that file? I know that using pm2 you can specify env vars in the ecosystem configuration but I would like to avoid doing that.
Same thing with docker; when building the image I specify some random values (project is open source, I would like to avoid having all those keys visible in the generated image), but it looks like when I try to run it with --env-file, it does not pick them up. (probably related to the same thing in the classic node server ?
Has anyone managed to make a setup like this work ?
2 Replies
Hi @Matteo G. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
In Nuxt, after your app is built for production, the
- Nuxt does not read
.env file is not automatically read when you start the server. This is by design, to ensure compatibility with various deployment environments (like serverless or edge platforms) that may not have a file system. Therefore, you must explicitly provide environment variables at runtime.
How to load .env in production
#### 1. With Node.js (including Docker)
If you want your Nuxt app to read from a .env file at runtime (e.g., when running node .output/server/index.mjs), you need to load the environment variables yourself. With Node.js 20+, you can use the --env-file flag:
This will load your .env file into process.env before starting your Nuxt server, making the variables available to your app at runtime. This approach works in Docker as well, as long as your image uses Node.js 20+ and the .env file is present in the container at the correct path. If you use an older Node.js version, you would need to use another tool (like dotenv-cli) or set the variables manually in your Dockerfile or entrypoint script.
Note: In Docker, the --env-file option for docker run sets environment variables for the container, but Nuxt will only pick up those variables if they are present in the environment when the server starts—not from a .env file inside the container unless you explicitly load it as above.
#### 2. With PM2
PM2 does not automatically load a .env file unless you specify the variables in your ecosystem.config.js file. If you want to avoid hardcoding secrets in your config, you can use PM2's env_file option:
This will load the variables from .env into the process environment when PM2 starts your app.
#### 3. Why doesn't Nuxt read .env in production?
Nuxt's built-in dotenv support only works during development, build, and generate time. In production, you are responsible for providing environment variables. This is a deliberate design choice for compatibility and security reasons. If you want the convenience of .env loading in production, you must use the methods above or similar tooling.
For convenience, in preview mode, yourNuxt Docs: nuxt preview.envfile will be loaded into process.env. (However, in production you will need to ensure your environment variables are set yourself. For example, with Node.js 20+ you could do this by runningnode --env-file .env .output/server/index.mjsto start your server.)
After your server is built, you are responsible for setting environment variables when you run the server. YourNuxt Docs: .env in production --- Summary:.envfiles will not be read at this point.
- Nuxt does not read
.env files in production by default.
- Use node --env-file .env .output/server/index.mjs (Node.js 20+) or PM2's env_file option to load .env at runtime.
- In Docker, ensure the environment variables are set at container start, or use the Node.js flag above if supported.
If you need more details on a specific setup (older Node.js, custom Docker entrypoint, etc.), let me know!
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@189947320261935105> Kapa.ai is still learning and improving, please let me know how I did by reacting below.