[英]Whenever I try to run my server via Heroku I get "Error: Cannot find module 'dotenv'"
Browsing stack overflow, I noticed that a common fix is make sure that dotenv is in dependencies and not just devDependencies.浏览堆栈溢出时,我注意到一个常见的修复方法是确保 dotenv 在依赖项中,而不仅仅是在 devDependencies 中。 Here's a screenshot of the log and the package.json .
这是日志和 package.json 的屏幕截图。 You'll notice that dotenv is indeed in my dependencies.
您会注意到 dotenv 确实在我的依赖项中。 I also deleted and reinstalled node_modules multiple times but still no luck.
我还多次删除并重新安装了 node_modules 但仍然没有运气。
I am requiring the dotenv at the top of my server file as shown here我需要服务器文件顶部的 dotenv,如此处所示
Can you post any necessary code please?您可以发布任何必要的代码吗?
I fixed the issue.我解决了这个问题。 I decided to try uninstalling dotenv and reinstalling, this worked.
我决定尝试卸载 dotenv 并重新安装,这很有效。 Apparently, this was different from deleting node modules and npm install.
显然,这与删除节点模块和 npm 安装不同。
I see dotenv
is added to a devDependency
: devDependency
are special and are only installed when you do a npm install
on the package.json
which contains them.我看到
dotenv
已添加到devDependency
中: devDependency
是特殊的,仅当您在包含它们的package.json
上npm install
时才会安装。 Usually, you'll add your development tooling (like test runners, etc.) in devDependencies
since they are not required for the application to run.通常,您将在
devDependencies
中添加您的开发工具(如测试运行程序等),因为它们不是应用程序运行所必需的。
When we run an application in production, we install dependencies using npm install --production
which only installs the dependencies mentioned in the dependencies
section of package.json
.当我们在生产中运行应用程序时,我们使用
npm install --production
安装依赖项,它只安装package.json
的依赖项部分中提到的dependencies
项。 This is also what Heroku does for you (check their buildpack for Node.js!)这也是 Heroku 为您所做的(查看他们的 Node.js 构建包!)
To install a devDependency
, you would do: npm i -D <package name>
;要安装
devDependency
,您可以: npm i -D <package name>
; and to install a production dependency
(like your web framework like express
), you'd do npm i -S express
.并安装生产
dependency
项(比如你的 web 框架,比如express
),你会做npm i -S express
。
Try running npm i -D dotenv
and check if it works (it may also work if you just do a npm i dotenv
.尝试运行
npm i -D dotenv
并检查它是否有效(如果您只执行npm i dotenv
也可能有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.