Search code examples
javascriptnode.jsenv-cmd

env-cmd Failed to find .rc file at default paths


I am using the tool env-cmd to manage my environment variables, and am encountering the error:

Error: Failed to find .rc file at default paths: [./.env-cmdrc,./.env-cmdrc.js,./.env-cmdrc.json]

I have verified that the file (in my case ./.env-cmdrc) does exist in the working directory and has sufficient permissions 644 (anyone can read). The file also contains the expected values.

This is not a result of using the wrong flag/syntax to find the env file as other questions are.

The command being ran is formatted as env-cmd -e ENVIRONMENT_NAME

Why is this error thrown?


Solution

  • Unfortunately, this library throws this misleading error in a few different cases when the file is actually present.

    Malformed JSON in the env file

    A commonly reported source of this error is invalid JSON in the file. (GitHub issue). Trailing commas are common, but it could be from a variety of causes.

    Missing environment in env file

    When using an env-cmdrc file with multiple environments (see Advanced Usage), this error is thrown when the environment specified in the command is not present in the file.

    In my case, I was using a variable to determine the target environment which was undefined, resulting in env-cmd -e undefined. The JSON did not have a key called undefined which causes the error to be thrown.


    These are the only two scenarios I have found that illegitimately cause this error so far. The library has not been updated in 3 years, so it seems unlikely that it will be fixed.