![translation](https://cdn.durumis.com/common/trans.png)
Questo è un post tradotto da IA.
[Next.js] Iniezione dell'ambiente di runtime
- Lingua di scrittura: Coreana
- •
-
Paese di riferimento: Tutti i paesi
- •
- Tecnologia dell'informazione
Seleziona la lingua
Testo riassunto dall'intelligenza artificiale durumis
- .env in Next.js viene impostato durante la compilazione, ma a volte è necessario impostare variabili ambientali in base all'ambiente di produzione (local, dev, qa, prod) dopo la compilazione.
- Gli utenti di Windows non possono utilizzare gli script basati su Unix esistenti, quindi devono installare cross-env per scrivere script che funzionino sia in Windows che in Unix.
- cross-env è stato applicato ai comandi start e dev per impostare le variabili ambientali e gli script sono stati riorganizzati in modo conciso per migliorare la leggibilità.
In Next.js, .env is hardcoded during the build process.
However, there are many needs to set the env when running the built image, not during the build process.
This is usually because we handle local, dev, qa, and prod.
So how can we do that?
Most of the context is similar to the article below.
However, the above article excludes Windows.
You can see that Unix syntax is used in the package.json script.
This means that clean, caring Windows users who don't use dirty Unix can't use this script.
Therefore, clean and caring Windows users should put a script that runs properly on both operating systems in consideration of Unix users.
The solution is simple.
Just put a script that does the same thing as this syntax.
Install cross-env with the command above.
And the start command mentioned above is solved by modifying it as follows.
But the script is too long to read and needs to be added to the dev command as well.
{
"dev": "npm run _env:-local && next dev",
"dev:dev": "npm run _env:dev && next dev",
"start": "npm run _env:-local && next start",
"_env:-local": "cross-env-shell node ./utils/cli.js next-env -e $APP_ENV",
"_env:dev": "node ./utils/cli.js next-env -e dev"
Therefore, I organized it as above.