Preview mode fails to run
When I want to run the build in preview mode:
The server carshes:
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
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:
ThePreview 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: - 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 thatpreview
command starts a server to preview your Nuxt application after running thebuild
command. Thestart
command is an alias forpreview
.
...
For convenience, in preview mode, your.env
file will be loaded intoprocess.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.mjs
to start your server.)
.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:
----------------------<@353654519365763072> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Yeah,
node .output/server/index.mjs
works fine but it doesn't load my custom .env file.