Heroku错误:找不到模块webpack。

9

heroku local web 可以正常工作,但是 heroku open 出现了 "Application error" 错误。

我检查了日志文件,发现报错信息是:找不到 webpack 模块。完整的日志文件如下:

Niteshs-MacBook-Pro:nyps nitesh$  heroku logs --tail
2016-06-16T03:40:53.132495+00:00 heroku[api]: Enable Logplex by nitesh@campusdiaries.com
2016-06-16T03:40:53.132551+00:00 heroku[api]: Release v2 created by nitesh@campusdiaries.com
2016-06-16T03:45:07.332337+00:00 heroku[api]: Deploy 690eea8 by nitesh@campusdiaries.com
2016-06-16T03:45:07.332405+00:00 heroku[api]: Release v3 created by nitesh@campusdiaries.com
2016-06-16T03:45:07.331714+00:00 heroku[api]: Scale to web=1 by nitesh@campusdiaries.com
2016-06-16T03:45:07.704674+00:00 heroku[slug-compiler]: Slug compilation finished
2016-06-16T03:45:07.704663+00:00 heroku[slug-compiler]: Slug compilation started
2016-06-16T03:45:14.763803+00:00 heroku[web.1]: Starting process with command `node server.js`
2016-06-16T03:45:17.000120+00:00 app[web.1]: module.js:341
2016-06-16T03:45:17.000129+00:00 app[web.1]:     throw err;
2016-06-16T03:45:17.000135+00:00 app[web.1]: 
2016-06-16T03:45:17.000135+00:00 app[web.1]:     ^
2016-06-16T03:45:17.000140+00:00 app[web.1]: Error: Cannot find module 'webpack'
2016-06-16T03:45:17.000141+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:339:15)
2016-06-16T03:45:17.000141+00:00 app[web.1]:     at Function.Module._load (module.js:290:25)
2016-06-16T03:45:17.000142+00:00 app[web.1]:     at Module.require (module.js:367:17)
2016-06-16T03:45:17.000142+00:00 app[web.1]:     at require (internal/module.js:20:19)
2016-06-16T03:45:17.000143+00:00 app[web.1]:     at Object.<anonymous> (/app/server.js:1:77)
2016-06-16T03:45:17.000144+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:422:10)
2016-06-16T03:45:17.000144+00:00 app[web.1]:     at Module._compile (module.js:413:34)
2016-06-16T03:45:17.000145+00:00 app[web.1]:     at Function.Module._load (module.js:314:12)
2016-06-16T03:45:17.000146+00:00 app[web.1]:     at Function.Module.runMain (module.js:447:10)
2016-06-16T03:45:17.000145+00:00 app[web.1]:     at Module.load (module.js:357:32)
2016-06-16T03:45:18.010833+00:00 heroku[web.1]: Process exited with status 1
2016-06-16T03:45:18.019746+00:00 heroku[web.1]: State changed from starting to crashed
2016-06-16T03:45:18.020059+00:00 heroku[web.1]: State changed from crashed to starting
2016-06-16T03:45:20.364946+00:00 heroku[web.1]: Starting process with command `node server.js`
2016-06-16T03:45:22.317904+00:00 app[web.1]: Error: Cannot find module 'webpack'
2016-06-16T03:45:22.317905+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:339:15)
2016-06-16T03:45:22.317899+00:00 app[web.1]: 
2016-06-16T03:45:22.317877+00:00 app[web.1]: module.js:341
2016-06-16T03:45:22.317905+00:00 app[web.1]:     at Function.Module._load (module.js:290:25)
2016-06-16T03:45:22.317892+00:00 app[web.1]:     throw err;
2016-06-16T03:45:22.317909+00:00 app[web.1]:     at Module.load (module.js:357:32)
2016-06-16T03:45:22.317906+00:00 app[web.1]:     at Module.require (module.js:367:17)
2016-06-16T03:45:22.317910+00:00 app[web.1]:     at Function.Module._load (module.js:314:12)
2016-06-16T03:45:22.317907+00:00 app[web.1]:     at require (internal/module.js:20:19)
2016-06-16T03:45:22.317908+00:00 app[web.1]:     at Module._compile (module.js:413:34)
2016-06-16T03:45:22.317907+00:00 app[web.1]:     at Object.<anonymous> (/app/server.js:1:77)
2016-06-16T03:45:22.317908+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:422:10)
2016-06-16T03:45:22.317898+00:00 app[web.1]:     ^
2016-06-16T03:45:22.317910+00:00 app[web.1]:     at Function.Module.runMain (module.js:447:10)
2016-06-16T03:45:23.310093+00:00 heroku[web.1]: Process exited with status 1
2016-06-16T03:45:23.327129+00:00 heroku[web.1]: State changed from starting to crashed
2016-06-16T03:45:30.105406+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=nyps.herokuapp.com request_id=096eccfb-7363-4137-88ef-c15389e7a664 fwd="106.51.20.194" dyno= connect= service= status=503 bytes=
2016-06-16T03:45:32.009229+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=nyps.herokuapp.com request_id=50951c24-4f9a-4347-963c-70c48560ab90 fwd="106.51.20.194" dyno= connect= service= status=503 bytes=
2016-06-16T03:55:36.073640+00:00 heroku[slug-compiler]: Slug compilation finished
2016-06-16T03:55:36.073633+00:00 heroku[slug-compiler]: Slug compilation started
2016-06-16T03:55:35.881416+00:00 heroku[api]: Deploy 85518b7 by nitesh@campusdiaries.com
2016-06-16T03:55:35.881515+00:00 heroku[api]: Release v4 created by nitesh@campusdiaries.com
2016-06-16T03:55:36.170187+00:00 heroku[web.1]: State changed from crashed to starting
2016-06-16T03:55:51.915142+00:00 heroku[web.1]: Starting process with command `node server.js`
2016-06-16T03:55:57.123301+00:00 app[web.1]: ==>   Listening on port 3000. Open up http://localhost:3000/ in your browser.
2016-06-16T03:55:56.884371+00:00 app[web.1]: process  production
2016-06-16T03:56:46.013013+00:00 heroku[web.1]: Error R14 (Memory quota exceeded)
2016-06-16T03:56:46.012815+00:00 heroku[web.1]: Process running mem=556M(108.8%)
2016-06-16T03:56:51.994793+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2016-06-16T03:56:51.994793+00:00 heroku[web.1]: Stopping process with SIGKILL
2016-06-16T03:56:53.386252+00:00 heroku[web.1]: State changed from crashed to starting
2016-06-16T03:56:53.364620+00:00 heroku[web.1]: Process exited with status 137
2016-06-16T03:56:53.383770+00:00 heroku[web.1]: State changed from starting to crashed
2016-06-16T03:57:01.236167+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=nyps.herokuapp.com request_id=3058277f-f351-4ff9-8cb7-e8500217bf1b fwd="106.51.20.194" dyno= connect= service= status=503 bytes=
2016-06-16T03:57:01.627497+00:00 heroku[web.1]: Starting process with command `node server.js`
2016-06-16T03:57:03.490555+00:00 app[web.1]: ==>   Listening on port 3000. Open up http://localhost:3000/ in your browser.
2016-06-16T03:57:03.405989+00:00 app[web.1]: process  production
2016-06-16T03:57:23.431249+00:00 app[web.1]: webpack built 114bd08e06394dc17ec8 in 19974ms
2016-06-16T03:58:01.964579+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=nyps.herokuapp.com request_id=ba97ba30-cc4b-493f-8907-4341a650b361 fwd="106.51.20.194" dyno= connect= service= status=503 bytes=
2016-06-16T03:58:02.073841+00:00 heroku[web.1]: Stopping process with SIGKILL
2016-06-16T03:58:02.073797+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2016-06-16T03:58:02.741937+00:00 heroku[web.1]: Process exited with status 137
2016-06-16T03:58:02.770625+00:00 heroku[web.1]: State changed from starting to crashed
2016-06-16T03:58:04.232803+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=nyps.herokuapp.com request_id=db9f5b88-805d-4917-9eb4-2d4827572afc fwd="106.51.20.194" dyno= connect= service= status=503 bytes=
2016-06-16T03:58:06.684479+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=nyps.herokuapp.com request_id=6b3b9797-f3d9-48e5-aebd-c157a2603596 fwd="106.51.20.194" dyno= connect= service= status=503 bytes=
2016-06-16T03:58:42.837687+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=nyps.herokuapp.com request_id=6a3ca1a5-ed23-416a-b3cf-3e755ee9e88e fwd="106.51.20.194" dyno= connect= service= status=503 bytes=
2016-06-16T03:58:46.216435+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=nyps.herokuapp.com request_id=52e5d646-ef1d-418e-8196-ddc79d68001b fwd="106.51.20.194" dyno= connect= service= status=503 bytes=
2016-06-16T04:04:10.169663+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=nyps.herokuapp.com request_id=b27e2500-1b84-4d8c-90d1-412a95ca3b13 fwd="106.51.20.194" dyno= connect= service= status=503 bytes=
2016-06-16T04:04:11.771412+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=nyps.herokuapp.com request_id=e7e7c74e-656c-4b85-abde-48cc54e3367c fwd="106.51.20.194" dyno= connect= service= status=503 bytes=

我在我的server.js的第一行调用var webpack = require('webpack'),并使用node server.js进行调用。
我删除了node_module;rm -rf node_module。然后执行了npm install,但仍面临相同的问题。

1
你的 package.json 依赖项中是否包含了 webpack?(不是 devDependencies - TheIronDeveloper
是的,你说得对。那就是问题所在。我很愿意接受这个答案。 - nitte93
好的,我马上会添加一个更简洁的答案。:+1: - TheIronDeveloper
2个回答

11

这更像是一个故障排除的答案,但通常出现以下两个问题之一:

  1. webpack被全局安装了(您曾经使用 npm install webpack -g 命令安装过)。
  2. webpack作为开发依赖项被安装。

如果您正在运行需要导入 webpack 的服务器,则应将其安装为 dependency

如果您没有在服务器端使用middleware,则通常将 webpack 设置为devDependency是您想要的。


3
尽管我符合你的两个条件,但我仍然遇到了这个错误。Webpack没有在我的系统上全局安装,而是在我的包依赖列表中。还有其他想法吗?这让我感到困惑。 - nicholas
1
@nicholas - 你找到解决方案了吗?我也遇到了同样的问题。 - Davor
将“webpack”列在依赖项(不是devDependencies)下,解决了我的问题。 - Ntokozo Zwane

1
我使用create-react-app创建的应用程序也遇到了同样的问题,我解决的方法是前往Heroku中我的应用程序页面的设置选项卡,在Buildpacks部分添加以下内容:https://buildpack-registry.s3.amazonaws.com/buildpacks/mars/create-react-app.tgz 作为构建包。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接