I am using NextJS. I need to create 3 type of environments:
I need to run on each env. specific variables.
For development i have .env, for productiuon i have .env.production and for staging i have .env.staging. My scrips looks like this:
"scripts": {
"dev": "next dev -p 3001",
"build": "next build",
"start": "next start",
},
For simulating staging env. i created this:
"start:staging": "NODE_ENV=staging next dev -p 3001",
But when i check console.log(process.env.NODE_ENV, 'env variable'); i get development.
What i do wrong and how to get the proper variables?
You are running next dev wich makes your application runs with developmento mode, thats why you get development.
Take a look at https://nextjs.org/docs/api-reference/cli#development
If you whant to use next.js build in env features you have the follow their docs https://nextjs.org/docs/basic-features/environment-variables#default-environment-variables.Basically you can have 3 environment files, local, development and production and they will be used when running the application with next dev and next start. The local env files always overrides the others, do not commit it to your project or your dev and production files wont be used.
If you want to do the exactly thing you said (development, staging and production) you will have to override next env by yourself. You can achieve this using dotenv.