Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hiredgunhouse opened this issue Apr 7, 2020 · 14 comments · May be fixed by #338
Open

Comments

@hiredgunhouse
Copy link

hiredgunhouse commented Apr 7, 2020

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.

@toddbluhm
Copy link
Owner

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 😄

@davegariepy
Copy link

thanks, in my case I had a trailing comma

@ztc212
Copy link

ztc212 commented Jul 23, 2020

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.

@tunglt1810
Copy link

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

@yue4u
Copy link

yue4u commented Sep 15, 2020

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.

@maximiliano-gt
Copy link

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

@AlsaadAhamed4
Copy link

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.

@alflennik
Copy link

alflennik commented Jul 13, 2021

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.

@bsides
Copy link

bsides commented Aug 19, 2021

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

@vjpr
Copy link

vjpr commented Aug 24, 2021

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'
}

@mankey-ru
Copy link

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: 8d94925

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)

@echosonusharma
Copy link

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

@benyap
Copy link

benyap commented Jun 15, 2022

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.

@mzenkner
Copy link

mzenkner commented Feb 7, 2023

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!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.