env-cmd error failed to locate ./.env file in gats

2020-02-10 05:21发布

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.

6条回答
Juvenile、少年°
2楼-- · 2020-02-10 05:26

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

查看更多
霸刀☆藐视天下
3楼-- · 2020-02-10 05:31

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
查看更多
太酷不给撩
4楼-- · 2020-02-10 05:35

Add -f to your package.json file

"develop": "env-cmd -f .env.development gatsby develop",
查看更多
一纸荒年 Trace。
5楼-- · 2020-02-10 05:41

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!

查看更多
你好瞎i
6楼-- · 2020-02-10 05:45

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
查看更多
Luminary・发光体
7楼-- · 2020-02-10 05:52

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.

查看更多
登录 后发表回答