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

This is driving me crazy: Error: Failed to find .env file at path: ./devOps/env/env.js

Open joaomelorodrigues opened this issue 2 years ago • 7 comments

I have created a env.js file that i want to use with dynamic vars (that's why i'm using js) I'm using the same logic in many other apps and it works fine with env-cmd But on a specific project that has the same files and same folder structure, basically everything is the same. It gives this error:

Error: Failed to find .env file at path: ./devOps/env/env.js

Everything i do i doens't work, i removed node_modules, package-lock, gitignore, dockerignore, every .ignore

running this command: env-cmd -f ./devOps/env/env.js react-scripts start

what am I doing wrong?

joaomelorodrigues avatar Oct 18 '23 09:10 joaomelorodrigues

same here

gganster avatar Oct 18 '23 14:10 gganster

Also my react app is completely ignoring env-cmd with a simple env-cmd -f .env.android react scripts build. @toddbluhm can you help?

joaomelorodrigues avatar Oct 26 '23 18:10 joaomelorodrigues

same. works for pnpm env:cmd not with npm

LizDodion avatar Jan 03 '24 23:01 LizDodion

Are you using this in an ESM setup? It seems the package currently doesn't support ESM and in this case fails with this error and hides the original one:

Error [ERR_REQUIRE_ESM]: require() of ES Module [...]\.env.js from [...]\node_modules\.pnpm\[email protected]\node_modules\env-cmd\dist\parse-env-file.js not supported.
.env.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules. 
Instead either rename .env.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in [...]\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead). 

    at Object.getEnvFileVars ([...]\node_modules\.pnpm\[email protected]\node_modules\env-cmd\dist\parse-env-file.js:21:33)

cyantree avatar Feb 09 '24 10:02 cyantree

Same here. Remarks:

  1. If I rename my env file from env.js to env.cjs, then I get rid of the message but the file is ignored. (this is even worst)
  2. A solution in my case was ro remove "type": "module" from the package.json of the folder and its parent.

caph1993 avatar Apr 19 '24 09:04 caph1993

I renamed the file to .cjs and it worked! Thanks @caph1993

jim-chien avatar Jul 10 '24 17:07 jim-chien