0

I have 2 files about .env

.env.local

STAGE=local
API=http://localhost:3333

.env.dev

STAGE=dev
API=https://dev.api.domain.com

I want to seperate them as package.json's script

What I want

"script": {
    "start-local": ".env.local && next",
    "start-dev": ".env.dev && next"
}

1 Answer 1

1

You can load a specific .env file by some env variable.

For example:

"script": {
    "start-local": "NODE_ENV=local next",
    "start-dev": "NODE_ENV=dev next"
}

This will define the NODE_ENV environment variable, then based on it, you can use dotenv lib to load the proper file.

// next.config.js
const dotEnv = require('dotenv');
const path = require('path');

const envFilePath = path.join(__dirname, `.env.${process.env.NODE_ENV}`); // this will have the path to the proper `.env` file

dotEnv.config({ path: envFilePath }); 

module.exports = {
  ...
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.