Home  >  Article  >  Backend Development  >  Can't read .env file without absolute path

Can't read .env file without absolute path

王林
王林forward
2024-02-05 11:49:35795browse

无法读取没有绝对路径的 .env 文件

Question content

When I try to read the .env file, it doesn't work without using an absolute path.

func init() {
    err := godotenv.load(".env") //<--fails
    //err := godotenv.load("./.env") //<--fails
    //err := godotenv.load("/home/peter/documents/tests/configuration/.env") //<--works
    if err != nil {
        panic(err)
    }
    config = getconfig()
}

I get panic: open .env: no such file or directory But the file is there

$ ls -a
.  ..  config.go  .env

Any clues?


Correct Answer


One potential problem is that the directory where the executable is run is different from the directory where the .env file is located. Can you verify this?

The above is the detailed content of Can't read .env file without absolute path. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete