admin管理员组文章数量:1426224
I am building a Nuxt application where I will have two environments staging and production, the local should also be considered as staging, now I need to create some mands and generate builds for production and staging, which will be deployed on two separate servers.
I have two questions
- The mand
npm run generate
always generate a production build, I checked it using
console.log(process.env.NODE_ENV)
How can I generate a new build where the env should be something like staging?
- I want to create some
.env
files for holding some env related variables, but I am confused about how can I create multiple env files for multiple envs (staging and production).
I understand my question is a bit of research orientation, I spent days researching on the internet, but either the blogs are unrelated or confusing. I never really got what I was looking for, can someone point me into the right direction?
I am building a Nuxt application where I will have two environments staging and production, the local should also be considered as staging, now I need to create some mands and generate builds for production and staging, which will be deployed on two separate servers.
I have two questions
- The mand
npm run generate
always generate a production build, I checked it using
console.log(process.env.NODE_ENV)
How can I generate a new build where the env should be something like staging?
- I want to create some
.env
files for holding some env related variables, but I am confused about how can I create multiple env files for multiple envs (staging and production).
I understand my question is a bit of research orientation, I spent days researching on the internet, but either the blogs are unrelated or confusing. I never really got what I was looking for, can someone point me into the right direction?
Share Improve this question edited May 10, 2022 at 20:22 kissu 47k16 gold badges90 silver badges189 bronze badges asked Dec 5, 2021 at 19:32 uneeb meeruneeb meer 9732 gold badges10 silver badges24 bronze badges 4-
NODE_env
needs to be defined on the staging platform, same goes for the env files. Meaning that if you want some variable locally, set it in.env
, if you want a specific variable for staging/production on Heroku, AWS or alike, set the env variable there (not in an.env
but on some dashboard/settings tab). – kissu Commented Dec 5, 2021 at 19:49 -
If I'm not mistaken,
generate
uses the defaultproduction
if nothing is passed. If you give it a specific value, either in.env
or by prefixingnpm run generate
, it should be good! – kissu Commented Dec 5, 2021 at 19:50 - @kissu I generated a build by defining NODE_ENV=staging in .env nothing changed still shows production when i create a build – uneeb meer Commented Dec 5, 2021 at 19:55
- It's because the build will overwrite the env variable at build time. – kissu Commented Dec 5, 2021 at 22:44
2 Answers
Reset to default 2use nuxt --dotenv
and pass the path to your env file like .env.production
Example package.json:
{
"scripts":{
"prod":"NODE_ENV=production && nuxt --dotenv .env.production"
}
}
As told above, you can do
NODE_ENV="staging" npm run generate
while building the app, even tho this is not remended and you should probably just use another env variable like ENV
and make conditionals based on this one for your app.
If you want more details on how to use env variables, you can look at this answer.
Then, the way env variables work still applies to what I said about AWS/Heroku etc...
本文标签: javascriptHow to create a build for multiple environments in NuxtStack Overflow
版权声明:本文标题:javascript - How to create a build for multiple environments in Nuxt? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745367222a2655573.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论