简体   繁体   English

错误:在Heroku中加载Express时找不到模块“ ./layer”

[英]Error: Cannot find module './layer' when loading Express in Heroku

I'm currently building a Node.js Express application on Heroku. 我目前正在Heroku上构建Node.js Express应用程序。 It works fine locally, but when I upload it to Heroku I get the following error: 它在本地工作正常,但是当我将其上传到Heroku时,出现以下错误:

2016-09-09T14:14:46.606481+00:00 app[web.1]: [DEBUG] Loading modules...
2016-09-09T14:14:46.607904+00:00 app[web.1]: [DEBUG] Loading express...
2016-09-09T14:14:46.632062+00:00 app[web.1]: module.js:457
2016-09-09T14:14:46.632064+00:00 app[web.1]:     throw err;
2016-09-09T14:14:46.632065+00:00 app[web.1]:     ^
2016-09-09T14:14:46.632065+00:00 app[web.1]:
2016-09-09T14:14:46.632066+00:00 app[web.1]: Error: Cannot find module './layer'

When I go to look in my local node_modules the file node_modules\\express\\lib\\router\\layer.js is present. 当我去查看本地node_modules时,会出现文件node_modules\\express\\lib\\router\\layer.js

I then ran heroku run bash and see : 然后,我运行heroku run bash并看到:

~/node_modules/express/lib/router $ ls
index.js  route.js

So I run: 所以我跑:

~ $ rm -rf node_modules/
~ $ npm i
~ $ npm start

And it all works fine again. 而且一切正常。 However logging out of heroku bash and it stops working. 但是注销heroku bash并停止工作。 After that if I then log back in and run npm start , it fails with the first message again: 之后,如果我重新登录并运行npm start ,它将再次失败,并显示第一条消息:

Running bash on little-owl... up, run.6609
~ $ npm start

> little-owl@0.0.1 start /app
> node server.js

[DEBUG] Loading modules...
[DEBUG] Loading express...
module.js:457
    throw err;
    ^

Error: Cannot find module './layer'

Any ideas? 有任何想法吗?

Heroku is not a VPS. Heroku不是VPS。 When you heroku run bash , you aren't making any permanent changes to your app's filesystem. heroku run bash ,您不会对应用程序的文件系统进行任何永久更改。 That's all done at build time, when you deploy. 当您进行部署时,一切都在构建时完成。

From the symptoms here, it looks like your app may have checked in some incomplete node_modules at some time in the past. 从这里的症状看,您的应用node_modules在过去的某个时间已经签入了一些不完整的node_modules So first, remove node_modules from git: 因此,首先,从git中删除node_modules

$ echo 'node_modules' >> .gitignore
$ git rm -r --cached node_modules
$ git commit -am 'ignore node_modules'

Then, disable your app's cache and push: 然后,禁用应用程序的缓存并推送:

$ heroku config:set NODE_MODULES_CACHE=false
$ git push heroku master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM