N
Nuxt2mo ago
Marcin

Preview mode fails to run

When I want to run the build in preview mode:
npx nuxi preview --dotenv .env
npx nuxi preview --dotenv .env
The server carshes:
╭──────────────────────────────────────────Preview Mode────────────────────────────────────────────╮
│ │
│ You are running Nuxt production build in preview mode. │
│ For production deployments, please directly use node {{ output.serverDir }}/index.mjs command. │
│ │
│ Node.js: v20.18.3 │
│ Nitro Preset: node-server │
│ Working directory: .output │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯

ℹ Loading .env. This will not be loaded when running the server in production.
ℹ Starting preview command: node {{ output.serverDir }}/index.mjs
╭──────────────────────────────────────────Preview Mode────────────────────────────────────────────╮
│ │
│ You are running Nuxt production build in preview mode. │
│ For production deployments, please directly use node {{ output.serverDir }}/index.mjs command. │
│ │
│ Node.js: v20.18.3 │
│ Nitro Preset: node-server │
│ Working directory: .output │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯

ℹ Loading .env. This will not be loaded when running the server in production.
ℹ Starting preview command: node {{ output.serverDir }}/index.mjs
It looks like the {{ output.serverDir }} is not being properly replaces. I think it started after updating to newest Nuxt v3 version. Any ideas?
3 Replies
kapa.ai
kapa.ai2mo ago
Hi @Marcin. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- It appears that when running npx nuxi preview --dotenv .env, the preview output is showing the literal string {{ output.serverDir }} instead of the expected path to your server entry file (e.g., .output/server/index.mjs). This suggests a templating or variable interpolation issue in the CLI output, likely introduced in a recent Nuxt update. From the knowledge sources, the correct behavior for the nuxt preview (or nuxi preview) command is to start a server using the built output, and it should reference the actual server entry file, not a template string. The documentation states:
The preview command starts a server to preview your Nuxt application after running the build command. The start command is an alias for preview.
...
For convenience, in preview mode, your .env file 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 running node --env-file .env .output/server/index.mjs to start your server.)
Preview command documentation The message you see is meant to remind you that for production you should use the Node.js command directly, but the placeholder should be replaced with the actual path. What to do: - This looks like a bug in the CLI output formatting, not in your project setup. - As long as the preview server starts and works, you can ignore the cosmetic issue. - If the server does not start, or you see further errors, try running the production server directly as suggested:
node .output/server/index.mjs

node .output/server/index.mjs

- If the problem persists after updating Nuxt, consider reporting it to the Nuxt team, as this may be a regression in the latest version. There is no explicit workaround or fix mentioned in the knowledge sources, but the issue is with the CLI message, not with the actual preview functionality or your environment setup. If you need to ensure your environment variables are loaded in production, remember that .env files are not loaded automatically in production mode—you must set them yourself or use the --env-file flag with Node.js 20+ as described above. If you need more details or if the preview server fails to start, please provide the full error output or logs. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@353654519365763072> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Marcin
MarcinOP2mo ago
Yeah, node .output/server/index.mjs works fine but it doesn't load my custom .env file.

Did you find this page helpful?