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
> gatsby-starter-hello-world@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\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.
You can set environment variable using your own custom
.env
files with-f
flag withenv-cmd
. Use this command to setenv
variables that are defined in custom file'./config/myvar.env'
.env-cmd -f ./config/myvar.env
For more information use this link
You can rename
.env.development
to just.env
and then runenv-cmd gatsby develop
, this will look for environment variables inside.env
file.You can also update the
develop
node script inside thepackage.json
like the following:Then you can run the node script,
or
Add
-f
to yourpackage.json
fileAdding the let snippet to the gatsby-config.js file did the trick for me! And then starting up with gatsby develop Tnx! Great help!
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)
Based on this gatsby-config.js, here is the develop script in package.json (unchanged) -
Finally, starting the gatsby app using
, the logs mentions playground being available -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.