【问题标题】:Error: Cannot find module './formats' when deploying node app on heroku server错误:在 Heroku 服务器上部署节点应用程序时找不到模块“./formats”
【发布时间】:2017-06-19 11:34:58
【问题描述】:

我在 heroku 上创建了免费帐户。我正在尝试在此部署节点应用程序,但这显示以下错误。

2017-06-19T11:25:53.680527+00:00 app[web.1]: > mypu@1.0.0 start /app
2017-06-19T11:25:53.680528+00:00 app[web.1]: > node server.js
2017-06-19T11:25:53.680528+00:00 app[web.1]: 
2017-06-19T11:25:53.862204+00:00 app[web.1]: module.js:471
2017-06-19T11:25:53.862207+00:00 app[web.1]:     throw err;
2017-06-19T11:25:53.862208+00:00 app[web.1]:     ^
2017-06-19T11:25:53.862209+00:00 app[web.1]: 
2017-06-19T11:25:53.862210+00:00 app[web.1]: Error: Cannot find module './formats'
2017-06-19T11:25:53.862211+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:469:15)
2017-06-19T11:25:53.862212+00:00 app[web.1]:     at Function.Module._load (module.js:417:25)
2017-06-19T11:25:53.862212+00:00 app[web.1]:     at Module.require (module.js:497:17)
2017-06-19T11:25:53.862213+00:00 app[web.1]:     at require (internal/module.js:20:19)
2017-06-19T11:25:53.862229+00:00 app[web.1]:     at Object.<anonymous> (/app/node_modules/qs/lib/stringify.js:4:15)
2017-06-19T11:25:53.862230+00:00 app[web.1]:     at Module._compile (module.js:570:32)
2017-06-19T11:25:53.862231+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:579:10)
2017-06-19T11:25:53.862231+00:00 app[web.1]:     at Module.load (module.js:487:32)
2017-06-19T11:25:53.862232+00:00 app[web.1]:     at tryModuleLoad (module.js:446:12)
2017-06-19T11:25:53.862233+00:00 app[web.1]:     at Function.Module._load (module.js:438:3)
2017-06-19T11:25:53.872899+00:00 app[web.1]: 
2017-06-19T11:25:53.886139+00:00 app[web.1]: npm ERR! Linux 3.13.0-112-generic
2017-06-19T11:25:53.886419+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2017-06-19T11:25:53.886602+00:00 app[web.1]: npm ERR! node v6.11.0
2017-06-19T11:25:53.886778+00:00 app[web.1]: npm ERR! npm  v3.10.10
2017-06-19T11:25:53.886955+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-06-19T11:25:53.887988+00:00 app[web.1]: npm ERR! mypu@1.0.0 start: `node server.js`
2017-06-19T11:25:53.888109+00:00 app[web.1]: npm ERR! Exit status 1
2017-06-19T11:25:53.888249+00:00 app[web.1]: npm ERR! 
2017-06-19T11:25:53.888373+00:00 app[web.1]: npm ERR! Failed at the mypu@1.0.0 start script 'node server.js'.
2017-06-19T11:25:53.888490+00:00 app[web.1]: npm ERR! Make sure you have the latest version of node.js and npm installed.
2017-06-19T11:25:53.888603+00:00 app[web.1]: npm ERR! If you do, this is most likely a problem with the mypu package,
2017-06-19T11:25:53.888719+00:00 app[web.1]: npm ERR! not with npm itself.
2017-06-19T11:25:53.889463+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2017-06-19T11:25:53.889583+00:00 app[web.1]: npm ERR!     node server.js
2017-06-19T11:25:53.889701+00:00 app[web.1]: npm ERR! You can get information on how to open an issue for this project with:
2017-06-19T11:25:53.889807+00:00 app[web.1]: npm ERR!     npm bugs mypu
2017-06-19T11:25:53.890108+00:00 app[web.1]: npm ERR! Or if that isn't available, you can get their info via:
2017-06-19T11:25:53.890263+00:00 app[web.1]: npm ERR!     npm owner ls mypu
2017-06-19T11:25:53.890433+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2017-06-19T11:25:53.898834+00:00 app[web.1]: 
2017-06-19T11:25:53.899088+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2017-06-19T11:25:53.899216+00:00 app[web.1]: npm ERR!     /app/npm-debug.log
Disconnected from log stream. There may be events happening that you do not see here! Attempting to reconnect...
2017-06-19T11:25:53.888719+00:00 app[web.1]: npm ERR! not with npm itself.
2017-06-19T11:25:53.889463+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2017-06-19T11:25:53.889583+00:00 app[web.1]: npm ERR!     node server.js
2017-06-19T11:25:53.889701+00:00 app[web.1]: npm ERR! You can get information on how to open an issue for this project with:
2017-06-19T11:25:53.889807+00:00 app[web.1]: npm ERR!     npm bugs mypu
2017-06-19T11:25:53.890108+00:00 app[web.1]: npm ERR! Or if that isn't available, you can get their info via:
2017-06-19T11:25:53.890263+00:00 app[web.1]: npm ERR!     npm owner ls mypu
2017-06-19T11:25:53.890433+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2017-06-19T11:25:53.898834+00:00 app[web.1]: 
2017-06-19T11:25:53.899088+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2017-06-19T11:25:53.899216+00:00 app[web.1]: npm ERR!     /app/npm-debug.log
Disconnected from log stream. There may be events happening that you do not see here! Attempting to reconnect...
Disconnected from log stream. There may be events happening that you do not see here! Attempting to reconnect...
2017-06-19T11:25:53.888719+00:00 app[web.1]: npm ERR! not with npm itself.
2017-06-19T11:25:53.889463+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2017-06-19T11:25:53.889583+00:00 app[web.1]: npm ERR!     node server.js
2017-06-19T11:25:53.889701+00:00 app[web.1]: npm ERR! You can get information on how to open an issue for this project with:
2017-06-19T11:25:53.889807+00:00 app[web.1]: npm ERR!     npm bugs mypu
2017-06-19T11:25:53.890108+00:00 app[web.1]: npm ERR! Or if that isn't available, you can get their info via:
2017-06-19T11:25:53.890263+00:00 app[web.1]: npm ERR!     npm owner ls mypu
2017-06-19T11:25:53.890433+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2017-06-19T11:25:53.898834+00:00 app[web.1]: 
2017-06-19T11:25:53.899088+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2017-06-19T11:25:53.899216+00:00 app[web.1]: npm ERR!     /app/npm-debug.log
Disconnected from log stream. There may be events happening that you do not see here! Attempting to reconnect...
2017-06-19T11:25:53.888719+00:00 app[web.1]: npm ERR! not with npm itself.
2017-06-19T11:25:53.889463+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2017-06-19T11:25:53.889583+00:00 app[web.1]: npm ERR!     node server.js
2017-06-19T11:25:53.889701+00:00 app[web.1]: npm ERR! You can get information on how to open an issue for this project with:
2017-06-19T11:25:53.889807+00:00 app[web.1]: npm ERR!     npm bugs mypu
2017-06-19T11:25:53.890108+00:00 app[web.1]: npm ERR! Or if that isn't available, you can get their info via:
2017-06-19T11:25:53.890263+00:00 app[web.1]: npm ERR!     npm owner ls mypu
2017-06-19T11:25:53.890433+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2017-06-19T11:25:53.898834+00:00 app[web.1]: 
2017-06-19T11:25:53.899088+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2017-06-19T11:25:53.899216+00:00 app[web.1]: npm ERR!     /app/npm-debug.log
Disconnected from log stream. There may be events happening that you do not see here! Attempting to reconnect...
2017-06-19T11:25:53.888719+00:00 app[web.1]: npm ERR! not with npm itself.
2017-06-19T11:25:53.889463+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2017-06-19T11:25:53.889583+00:00 app[web.1]: npm ERR!     node server.js
2017-06-19T11:25:53.889701+00:00 app[web.1]: npm ERR! You can get information on how to open an issue for this project with:
2017-06-19T11:25:53.889807+00:00 app[web.1]: npm ERR!     npm bugs mypu
2017-06-19T11:25:53.890108+00:00 app[web.1]: npm ERR! Or if that isn't available, you can get their info via:
2017-06-19T11:25:53.890263+00:00 app[web.1]: npm ERR!     npm owner ls mypu
2017-06-19T11:25:53.890433+00:00 app[web.1]: npm ERR! There is likely additional logging output above.

相同的应用程序在我的本地机器上运行良好。这是package.json

{
  "name": "mypu",
  "version": "1.0.0",
  "description": "mypu",
  "main": "index.js",
  "scripts": {
    "start" : "node server.js"
  },
  "repository": {
    "type": "git",
    "url": ""
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": ""
  },
  "homepage": "",
  "dependencies": {
    "body-parser": "^1.15.0",
    "connect-timeout": "^1.8.0",
    "express": "^4.13.4",
    "generic-pool": "^2.4.2",
    "log4js": "^1.1.1",
    "multer": "^1.2.0",
    "mysql": "^2.10.2",
    "requestify": "^0.1.17"
  }
}

可能是什么问题?我错过了什么吗?

【问题讨论】:

    标签: node.js heroku


    【解决方案1】:

    是的,这个错误是由于缺少模块而发生的,就像您的情况一样错误:找不到模块'./formats'。您必须清理现有的 node_modules 目录并运行 npm install 全新安装所有依赖项。如果您仍然在部署中遇到问题,请通过此链接了解更多详细信息

    https://devcenter.heroku.com/articles/deploying-nodejs

    https://scotch.io/tutorials/how-to-deploy-a-node-js-app-to-heroku

    https://www.youtube.com/watch?v=6Fu39V6T_G0

    【讨论】:

      【解决方案2】:

      删除 node_modules 的缓存并重新部署它:

      git rm -r --cached node_modules
      

      然后git push heroku master

      如果您不想在重新部署时缓存 node_module,也可以禁用缓存:

      heroku config:set NODEMODULESCACHE=false
      git commit -am 'rebuild' --allow-empty
      git push heroku master
      heroku config:unset NODEMODULESCACHE
      

      这对我有用!

      【讨论】:

        【解决方案3】:

        就我而言,我的存储库中有node_modules 目录。我将其从 github 存储库中排除并再次部署。现在它正在工作

        【讨论】:

          猜你喜欢
          • 2014-10-21
          • 1970-01-01
          • 2014-04-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-04
          • 2018-09-11
          相关资源
          最近更新 更多