env-cmd icon indicating copy to clipboard operation
env-cmd copied to clipboard

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

Open hiredgunhouse opened this issue 4 years ago • 14 comments

This has been reported already in #118 but the OP closed it. The reason for this error message in both my case in issue 118's OP was that the .env-cmd was not valid JSON and could not be parsed correctly which would have been much more helpful error message than this confusing information about env-cmd not being able to find the .rc file. The issue lies here in get-env-var.js inside the catch that is muting all exceptions except EnvironmentError, I added a comment and console.log there to debug locally:

    for (const path of RC_FILE_DEFAULT_LOCATIONS) {
        try {
            const env = await parse_rc_file_1.getRCFileVars({ environments, filePath: path });
            if (verbose === true) {
                console.info(`Found environments: [${environments.join(',')}] for default .rc file at path: ${path}`);
            }
            return env;
        }
        catch (e) {
            // not sure why you want mute exceptions other than "EnvironmentError", add the console.log in the line below to see the true error being muted.
            console.log(`ERROR: ${e}`);
            if (e.name === 'EnvironmentError') {
                const errorText = `Failed to find environments: [${environments.join(',')}] for .rc file at path: ${path}`;
                if (verbose === true) {
                    console.info(errorText);
                }
                throw new Error(errorText);
            }
        }
    }

Maybe you could just rethrow the error below the "if" statement to make sure errors are not muted.

hiredgunhouse avatar Apr 07 '20 13:04 hiredgunhouse

Thanks for bringing this up. I need to do some refactoring around how error handling/propagation works anyways. I will fix this while I am at it 😄

toddbluhm avatar Apr 22 '20 19:04 toddbluhm

thanks, in my case I had a trailing comma

davegariepy avatar May 27 '20 20:05 davegariepy

I just had the same problem. Just a thought: disallow .env-cmdrc w/o a type suffix? At least in that case lint will catch the error for many.

ztc212 avatar Jul 23 '20 19:07 ztc212

sometime it's work, sometime .... Can i change to older version to fix this issues

tunglt1810 avatar Aug 03 '20 15:08 tunglt1810

I had the same issue and using a single environment is throwing environments.forEach is not a function

TypeError: environments.forEach is not a function
    at Object.getRCFileVars (/Users/yue/Desktop/dev/node_modules/env-cmd/dist/parse-rc-file.js:43:18)
    at async getRCFile (/Users/yue/Desktop/dev/node_modules/env-cmd/dist/get-env-vars.js:65:25)
    at async Object.EnvCmd (/Users/yue/Desktop/dev/node_modules/env-cmd/dist/env-cmd.js:39:15)
    at async Object.CLI (/Users/yue/Desktop/dev/node_modules/env-cmd/dist/env-cmd.js:19:16)

As a workaround for now, I have to use -r option to specific which rc file to use and append a trailing comma if using only one environment

for example:

env-cmd -r .env-cmdrc -e production, node server.js

this project helps me a lot and I really like it. hoping this can be fixed.

yue4u avatar Sep 15 '20 11:09 yue4u

any news about this? same issue

My .env-cmdrc.json file looks like

{ "development": { "REACT_APP_BASE_PAGE_URL": "https://devurl.com" }, "production": { "REACT_APP_BASE_PAGE_URL": "https://produrl.com" } }

script : env-cmd --rc-file ./.env-cmdrc.json -e development, react-scripts build

Error: Error [PathError]: Failed to find .rc file at path: /myfolder/.env-cmdrc.json

maximiliano-gt avatar Oct 14 '20 10:10 maximiliano-gt

Hi, I faced the same error message. The issue was with the incorrect JSON object written in the file. If you have done some typo in .env-cmdrc file you'll get this error message. Typo in the sense missed quotes or colon ... etc.

AlsaadAhamed4 avatar Feb 25 '21 04:02 AlsaadAhamed4

This confused me because I thought env-cmd couldn't find my configuration file, since that was the only error I saw. It turned out it was an error in my configuration code causing the issue. I'd definitely suggest outputting any errors to the console.

alflennik avatar Jul 13 '21 19:07 alflennik

The same error happens when trying to use type: "module" (ES Module) in package.json

bsides avatar Aug 19 '21 13:08 bsides

The same error happens when trying to use type: "module" (ES Module) in package.json

Here is the error. Need to use a dynamic import in getEnvFileVars.

Error [ERR_REQUIRE_ESM]: require() of ES Module /xxx/node_modules/.pnpm/[email protected]/node_modules/env-cmd/dist/parse-env-file.js not supported.
Instead change the require of .env.js in /xxx/.pnpm/[email protected]/node_modules/env-cmd/dist/parse-env-file.js to a dynamic import() which is available in all CommonJS modules.
    at Object.getEnvFileVars (/xxx/.pnpm/[email protected]/node_modules/env-cmd/dist/parse-env-file.js:21:33)
    at getEnvFile (/xxx/.pnpm/[email protected]/node_modules/env-cmd/dist/get-env-vars.js:46:48) {
  code: 'ERR_REQUIRE_ESM'
}

vjpr avatar Aug 24 '21 13:08 vjpr

The same error happens when trying to use type: "module" (ES Module) in package.json

Here is the error. Need to use a dynamic import in getEnvFileVars.

Error [ERR_REQUIRE_ESM]: require() of ES Module /xxx/node_modules/.pnpm/[email protected]/node_modules/env-cmd/dist/parse-env-file.js not supported.
Instead change the require of .env.js in /xxx/.pnpm/[email protected]/node_modules/env-cmd/dist/parse-env-file.js to a dynamic import() which is available in all CommonJS modules.
    at Object.getEnvFileVars (/xxx/.pnpm/[email protected]/node_modules/env-cmd/dist/parse-env-file.js:21:33)
    at getEnvFile (/xxx/.pnpm/[email protected]/node_modules/env-cmd/dist/get-env-vars.js:46:48) {
  code: 'ERR_REQUIRE_ESM'
}

It seems much more easy and bulletproof to fix the problem with '.cjs' extension, which is done for ENV JS files: https://github.com/toddbluhm/env-cmd/commit/8d94925a445506aef3bbc1fadbb984c7a98eba0c

I'd like to make similar '.cjs' fix for RC JS file too (now there is no way to make it work in 'type: module' project)

mankey-ru avatar Jan 26 '22 19:01 mankey-ru

its most likely a syntax error for me my env-cmdrc had a comma in the last key value pair in the development section.

echosonusharma avatar Jan 28 '22 14:01 echosonusharma

Any update on this one? No updates from @toddbluhm since 2020 :(

Another situation where this is a problem is when you are returning a promise from .env.js and it throws an error. Like the other situations, it would be much more helpful if the error thrown by the promise was surfaced instead of the "Failed to find .env file" message which is very misleading.

benyap avatar Jun 15 '22 01:06 benyap

I spent an entire workday working through this issue. My build passed on local Docker but failed with the above error when running in ECR with Argo. Changing this from a .js to .json file fixed this for me. Not sure whether to scream or laugh!!

mzenkner avatar Feb 07 '23 00:02 mzenkner