javascript env-cmd 错误无法在 gatsby 中找到 ./.env 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56301852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
env-cmd error failed to locate ./.env file in gatsby?
提问by Krishna Kamal
I have a file name .env.development in the root folder. I had install env-cmd as dev dependencies
when I start the server
我在根文件夹中有一个文件名 .env.development 。我在
启动服务器时安装了 env-cmd 作为开发依赖项
> 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.
(节点:1368)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。(rejection id: 2) (node:1368) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的承诺拒绝将使用非零退出代码终止 Node.js 进程。
回答by Ajay Yadav
You can set environment variable using your own custom .envfiles with -fflag with env-cmd. Use this command to set envvariables that are defined in custom file './config/myvar.env'.
您可以使用.env带有-f标志的自定义文件设置环境变量env-cmd。使用此命令设置env自定义文件中定义的变量'./config/myvar.env'。
env-cmd -f ./config/myvar.env
env-cmd -f ./config/myvar.env
For more information use this link
有关更多信息,请使用此链接
回答by rohit
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
这已在最新版本的 env-cmd 中进行了更新,如果您使用的是 <9.0.0 版本,那么它将完美运行,但对于版本 >9.0.0,它将寻找的默认环境文件是 .env
use env-cmd -f .env.development gatsby developinstead, here -f is provided for custom file name.
env-cmd -f .env.development gatsby develop改为使用,这里 -f 用于自定义文件名。
回答by raja
Add -fto your package.jsonfile
添加-f到您的package.json文件
"develop": "env-cmd -f .env.development gatsby develop",
回答by mbaljeetsingh
You can rename .env.developmentto just .envand then run env-cmd gatsby develop, this will look for environment variables inside .envfile.
您可以重命名.env.development为 just.env然后运行env-cmd gatsby develop,这将在.env文件中查找环境变量。
You can also update the developnode script inside the package.jsonlike the following:
您还可以更新develop节点脚本package.json,如下所示:
"develop": "env-cmd gatsby develop"
Then you can run the node script,
然后你可以运行节点脚本,
npm run develop
or
或者
gatsby develop
回答by Amjad Desai
use the -f flag and make sure the path to your .env.development file is correct.
使用 -f 标志并确保 .env.development 文件的路径正确。
"develop": "env-cmd -f ./.env.development gatsby develop"
回答by Amit Misra
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)
遇到了同样的问题,这是我添加的 gatsby-config.js 片段,以使 gatsby 可以看到“.env.development”文件。(不确定这是否是唯一的方法,请节点/盖茨比专家插话)
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) -
基于此 gatsby-config.js,这是 package.json 中的开发脚本(未更改)-
...
"develop": "gatsby develop",
...
Finally, starting the gatsby app using
最后,使用以下命令启动 gatsby 应用程序
npm run develop,日志中提到了可用的操场-
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
回答by Juan Silupú Maza
In Windows.
First create file .env.developmentThen add in package.json:
"develop": "env-cmd -f .env.development --fallback gatsby develop"and: npm run developattached: https://css-tricks.com/using-graphql-playground-with-gatsby
在 Windows 中。首先创建文件.env.development然后添加 package.json:
"develop": "env-cmd -f .env.development --fallback gatsby develop"和:npm run develop附加: https://css-tricks.com/using-graphql-playground-with-gatsby
回答by Takki Takki
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!
将 let 片段添加到 gatsby-config.js 文件对我来说很有效!然后从 gatsby 开始开发 Tnx!很大的帮助!
回答by Simbarashe Maunga
Step 1:
First run the command: npm install --save-dev [email protected]
第一步:首先运行命令: npm install --save-dev [email protected]
Step 2:
Replace env-cmd .env-development gatsby developwith ./node_modules/.bin/env-cmd -f ./.env.development gatsby develop
第 2 步:替换env-cmd .env-development gatsby develop为./node_modules/.bin/env-cmd -f ./.env.development gatsby develop
回答by Adewale Olaoye
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.
并传入这个值 .env.development 文件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
如果您想了解如何更好地设置它,可以查看Adebiyi Adedotun撰写的关于 CSS Tricks 的 这篇文章

