Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrisgrim's avatar

Custom .env variable for npm run production

Hi All, I am currently switching my images from hosting them on my project server to digital ocean spaces. I have everything working great using flysystem-aws-s3-v3 but I am now looking into the best way to have a testing and a production image server. When I switched to spaces I had to update my Vue project to use a custom url instead of my public folder. I did this using the .env mix. in my .env file:

MIX_IMAGE_URL=https://DEV.sfo3.digitaloceanspaces.com/public/

The issue is vue compiles locally so while laravel will use the production .env, vue will be using my local .env. Is there a way that I can say when running npm run prod it uses a different location? Like in my .env file

if('production')
{  
		MIX_IMAGE_URL=https://DEV.sfo3.digitaloceanspaces.com/public/ 
} else {
		MIX_IMAGE_URL=https://PROD.sfo3.digitaloceanspaces.com/public/ 
}
0 likes
13 replies
MohamedTammam's avatar

@chrisgrim In your project use two .env files. one named .env.local and other named .env.production When you compile use assets using MIX. if you run npm run dev MIX is going to use the variables in your .env.local. When you build for production and run npm run prod MIX will use the variables in your .env.production.

chrisgrim's avatar

@MohamedTammam I thought that is how it would work but if I just have an .env.local and an .env.production when I go to my site I get the error

No application encryption key has been specified. {"exception":"[object] (Illuminate\Encryption\MissingAppKeyException(code: 0): No application encryption key has been specified. at /

In order to get my app to run I have to rename .env.local back to just .env.

chrisgrim's avatar

I should also note if I have a .env and a .env.local and .env.production when I run npm run dev or npm run prod it still uses my .env instead of the other two.

MohamedTammam's avatar

@chrisgrim Your production js files doesn't related to .env in production. .env should be used for back-end only in that case.

chrisgrim's avatar

@MohamedTammam in my .env file I use

MIX_IMAGE_URL=https://DEV.sfo3.digitaloceanspaces.com/public/

which is cool because when I run npm dev it grabs that url and puts it into my js. However I need to have it so when I am running npm run prod it uses a different url

MIX_IMAGE_URL=https://PROD.sfo3.digitaloceanspaces.com/public/

since I have two servers holding images, my DEV and my PROD.

MohamedTammam's avatar

@chrisgrim In your .env.local add

MIX_IMAGE_URL=https://DEV.sfo3.digitaloceanspaces.com/public/

In your .env.prod add

MIX_IMAGE_URL=https://PROD.sfo3.digitaloceanspaces.com/public/
chrisgrim's avatar

@MohamedTammam

I have done that. So I only have a .env.local and a .env.production in my local project folder. However when I try to access the app it errors out because laravel needs a .env file.

So I added the .env back so now I have a .env, .env.local and .env.production in my project folder. However if I run npm run production it uses my .env file instead of my .env.production file

Please or to participate in this conversation.