简体   繁体   English

每当我尝试通过 Heroku 运行我的服务器时,我都会收到“错误:找不到模块 'dotenv'”

[英]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.jsonnpm 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.

相关问题 每当我尝试使用 Next.js 运行“npm run dev”时,都找不到错误模块 - ERROR MODULE NOT FOUND whenever I try to run 'npm run dev' using Next.js 我收到路径错误:[找不到模块] 每当我尝试在 VS Code 中运行任何与 npm 相关的命令时 - I got path error : [module not found] whenever I try to run any npm related command in VS Code 将 node.js 应用程序部署到 heroku 时出错(找不到模块 dotenv 和 npm ERR!errno 1) - Error while deploying node.js application to heroku (cannot find module dotenv, and npm ERR! errno 1) Heroku Scheduler-当我尝试运行heroku运行节点时,出现错误 - Heroku Scheduler - When I try to run heroku run node, I get an error 尝试要求我的javascript文件时找不到模块“ models / PermissionModel”错误 - Cannot find module 'models/PermissionModel' error when I try to require my javascript file 每当我尝试在 Visual Studio 代码上运行“npm install”时出现错误 - I get an error whenever i try to run "npm install" on visual studio code 如果我尝试节点hello.js,则找不到模块是错误 - cannot find module is the error if i try node hello.js 如何摆脱错误:无法在Heroku上找到模块&#39;模型&#39; - How can I get rid of Error: Cannot find module 'models' on Heroku 我正在使用 node js 开发一个应用程序,它在本地运行良好,但在我尝试将其上传到 heroku 时崩溃,它说 cat find dotenv - i am developing an app with node js , it works perfectly locally but crashes anytime i try to upload it to heroku,it says cat find dotenv 当我在终端中运行“gulp”时,为什么会出现:“错误:找不到模块 sass”? - Why do I get: "Error: cannot find module sass" when I run "gulp" in terminal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM