toddbluhm / env-cmd

Setting environment variables from a file
https://www.npmjs.com/package/env-cmd
MIT License
1.73k stars 65 forks source link

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

Open cyango opened 10 months ago

cyango commented 10 months ago

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?

gganster commented 10 months ago

same here

cyango commented 10 months ago

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

LizDodion commented 7 months ago

same. works for pnpm env:cmd not with npm

cyantree commented 6 months ago

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\env-cmd@10.1.0\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\env-cmd@10.1.0\node_modules\env-cmd\dist\parse-env-file.js:21:33)
caph1993 commented 4 months ago

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.
jim-chien commented 1 month ago

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