Running 'npm run' with specific env file using dotenv

Viewed 52

I am currently working on a reactjs project which has been created with create-react-app. env-cmd package is a little too old and is not under maintenance anymore so I'd like to use dotenv instead. Is there a way that I can specify a .env file just like we usually do with env-cmd but with the help of dotenv instead? Or is there a suggested method I should be following using dotenv?

Thanks in advance.

1 Answers

You can specify the path with .config({path: "directory"})

    // Using require syntax    
    require("dotenv").config({ path: "../.env" });
    
    // Using import syntax
    import dotenv from "dotenv"
    dotenv.config({path: "../.env"})
Related