Question

[Solved] env-cmd error failed to locate ./.env file in gatsby?

I have a file name .env.development in the root folder. I had install env-cmd as dev dependencies
when I start the server

  > npm run develop

its give me an error

> [email protected]0.1.0 develop I:learngatsby
> env-cmd .env-development gatsby develop

(node:1368) UnhandledPromiseRejectionWarning: Error: Unable to locate env file at default location (./.env)
at I:learngatsby
ode_modulesenv-cmddistget-env-vars.js:44:19
at Generator.throw (<anonymous>)
at rejected (I:learngatsby
ode_modulesenv-cmddistget-env-vars.js:5:65)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
(node:1368) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:1368) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Solution #1:

You can set environment variable using your own custom .env files with -f flag with env-cmd. Use this command to set env variables that are defined in custom file './config/myvar.env'.

env-cmd -f ./config/myvar.env

For more information use this link

Respondent: Ajay Yadav

Solution #2:

This has been updated in the latest version of env-cmd, if you are using version <9.0.0 then it will work perfectly but with version >9.0.0 the default environment file it will look for is .env

use env-cmd -f .env.development gatsby develop instead, here -f is provided for custom file name.

Respondent: rohit

Solution #3:

Add -f to your package.json file

"develop": "env-cmd -f .env.development gatsby develop",
Respondent: raja

Solution #4:

You can rename .env.development to just .env and then run env-cmd gatsby develop, this will look for environment variables inside .env file.

You can also update the develop node script inside the package.json like the following:

"develop": "env-cmd gatsby develop"

Then you can run the node script,

npm run develop

or

gatsby develop
Respondent: mbaljeetsingh

Solution #5:

use the -f flag and make sure the path to your .env.development file is correct.

"develop": "env-cmd -f ./.env.development gatsby develop"
Respondent: Amjad Desai

Solution #6:

In Windows.
First create file .env.development
Then add in package.json:
"develop": "env-cmd -f .env.development --fallback gatsby develop"
and: npm run develop
attached: https://css-tricks.com/using-graphql-playground-with-gatsby

Solution #7:

Ran into the same issue, here’s a snippet of gatsby-config.js I added to make the “.env.development” file visible to gatsby. (Not sure if this is the only way, node/Gatsby experts please chime in)


let activeEnv =
  process.env.GATSBY_ACTIVE_ENV || process.env.NODE_ENV || "development"

console.log('Using environment config: ${activeEnv}')

require("dotenv").config({
  path: `.env.${process.env.NODE_ENV}`,
})

Based on this gatsby-config.js, here is the develop script in package.json (unchanged) –


...
"develop": "gatsby develop",
...

Finally, starting the gatsby app using

npm run develop

, the logs mentions playground being available –


You can now view gatsby-starter-hello-world in the
â €
  http://localhost:8000/
â €
View the GraphQL Playground, an in-browser IDE, to
â €
  http://localhost:8000/___graphql
Respondent: Amit Misra

Solution #8:

Step 1:
First run the command: npm install --save-dev [email protected]

Step 2:
Replace env-cmd .env-development gatsby develop with ./node_modules/.bin/env-cmd -f ./.env.development gatsby develop

Respondent: Simbarashe Maunga

Solution #9:

Adding the let snippet to the gatsby-config.js file did the trick for me! And then starting up with gatsby develop
Tnx! Great help!

Respondent: Takki Takki

Solution #10:

Thanks, everyone.
This solves it for me

"develop": "env-cmd --file .env.development --fallback gatsby develop",

and pass in this value .env.development file GATSBY_GRAPHQL_IDE=playground.

In case, you want to understand how to set it up better, You can check out this article
on CSS Tricks by
Adebiyi Adedotun

Respondent: Adewale Olaoye

Solution #11:

I had same problem,

i tried below code

env-cmd -f ./config/myvar.env

its not working for me.

Instead i add full path of config folder like below and its works like charm..!!!!

env-cmd -f fullPath/config/myvar.env
Respondent: satyawan

Solution #12:

In my case got this error :

Error: Failed to find .env file at path: .env.local at getEnvFile (E:project....)

Then i just rename .env.development file to .env.local and it is working fine.

or if you are working on remote git repository then check you have pulled latest changes made by your team member to your local dev environment.

Respondent: akshay_sushir

Solution #13:

In my case it works having .env file with following scripts for develop inside the package.json.

"develop": "env-cmd gatsby develop"
Respondent: Vishwanath Sinha

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy