Krishna Kamal
Krishna Kamal

Reputation: 771

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] 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\node_modules\env-cmd\dist\get-env-vars.js:44:19
at Generator.throw (<anonymous>)
at rejected (I:\learngatsby\node_modules\env-cmd\dist\get-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.

Upvotes: 32

Views: 40941

Answers (14)

Arjun Regmi
Arjun Regmi

Reputation: 31

Add -f to your package.json file.

"dev": "env-cmd -f ./config/dev.env nodemon src/index.js"

Upvotes: 3

Vishwanath Sinha
Vishwanath Sinha

Reputation: 11

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

"develop": "env-cmd gatsby develop"

Upvotes: 0

akshay_sushir
akshay_sushir

Reputation: 1891

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.

Upvotes: 0

satyawan
satyawan

Reputation: 507

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

Upvotes: 0

Juan Silup&#250; Maza
Juan Silup&#250; Maza

Reputation: 991

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

Upvotes: 7

Simbarashe Maunga
Simbarashe Maunga

Reputation: 31

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

Upvotes: 3

Amjad Desai
Amjad Desai

Reputation: 91

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

"develop": "env-cmd -f ./.env.development gatsby develop"

Upvotes: 8

Adewale Olaoye
Adewale Olaoye

Reputation: 101

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

Upvotes: 0

raja
raja

Reputation: 371

Add -f to your package.json file

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

Upvotes: 35

Ajay Yadav
Ajay Yadav

Reputation: 741

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

Upvotes: 74

rohit
rohit

Reputation: 538

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.

Upvotes: 42

Takki Takki
Takki Takki

Reputation: 1

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!

Upvotes: 0

Amit Misra
Amit Misra

Reputation: 371

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

Upvotes: 3

mbaljeetsingh
mbaljeetsingh

Reputation: 449

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

Upvotes: 13

Related Questions