如何找出 Node.js 项目中缺少的依赖项 [英] How do I figure out what dependency is missing in a Node.js project

查看:151
本文介绍了如何找出 Node.js 项目中缺少的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的 Node.js 项目部署到 OpenShift,但在尝试查看站点时收到 503.

I am trying to deploy my Node.js project to OpenShift and I get 503 when trying to view the site.

我从研究中了解到这表明我的网络服务器项目存在问题.通过检查日志,我确定这似乎是缺少依赖项的问题.但是,日志似乎没有告诉我是哪一个:

I am aware from research that this indicates a problem with my webserver project. By checking the logs, I have determined that this appears to be an issue of a missing dependency. However, the logs don't appear to tell me which one:

module.js:340
    throw err;
          ^
Error: Cannot find module '/var/lib/openshift/551c63f85973ca05430002b8/app-root/runtime/repo/server.js'

我已验证我的网络服务器需要"的每个包都存在于 package.json 文件中,并且存在于 node_modules 文件夹中.

I have verified that every package which is "required" by my webserver is present in both the package.json file and is present in the node_modules folder.

{
  "name": "NekoList",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "bcrypt-nodejs": "0.0.3",
    "body-parser": "~1.12.4",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "ejs": "~2.3.1",
    "express": "~4.12.4",
    "mongoose": "^4.0.3",
    "morgan": "~1.5.3",
    "node-gyp": "^2.0.1",
    "secure-random": "^1.1.1",
    "serve-favicon": "~2.2.1"
  }
}

在运行 npm installnpm update 时,我没有遇到任何错误.我确实收到了关于 debug ~2.2.0 的未满足依赖项的警告,但我在另一个项目中遇到了同样的警告,并且它不会导致严重故障.我没有使用此模块的代码.它就在那里,因为它是模板的一部分.

When running an npm install or npm update I get no errors. I do get warnings about an unmet dependency on debug ~2.2.0, but I have this same warning in another project and it doesn't cause a critical failure. I have no code that uses this module. It's just there because it was part of the template.

minimist@0.0.8 node_modules/node-gyp/node_modules/mkdirp/node_modules/minimist
npm WARN unmet dependency /var/lib/openshift/551c63f85973ca05430002b8/app-root/runtime/repo/node_modules/express/node_modules/finalhandler requires debug@'~2.2.0' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined

此外,该项目在使用 Webstorm 内置服务器的本地开发环境中运行良好.

Also, the project runs fine on my local development environment using the Webstorm built-in server.

我想知道我可以做些什么来修复,或者至少阐明问题的根源.

I am looking to know what I might do to fix, or at least shed some light on the root of the issue.

提前致谢.

因此,根据 Saba Hassan 所说的,节点似乎正在尝试运行不存在的文件 server.js,而不是我定义的入口点 bin/www

So, based on what Saba Hassan is saying, it looks like node is trying to run the nonexistant file server.js, instead of the entry point that I have defined, bin/www

因此,我在基本示例服务器中添加了一个名为 server.js 的文件.Openshift 仍然找不到该文件.同样的错误信息.[/编辑]

So, I added a file called server.js with the basic example server. Openshift still cannot find the file. Same error message. [/Edit]

【解决方法】我已经得出结论,这个问题的基本假设是不正确的.我赞同一个解释为什么 OpenShift 在错误的位置寻找插入点的答案.但是,我开始这个线程是为了弄清楚为什么我会得到一个缺失的依赖错误.即使在实施以下解决方案后,我仍然收到相同的错误消息,关于缺少依赖项.

[Solution] I have concluded that the basic assumption of this question is incorrect. I endorsed an answer that explains why OpenShift is looking in the wrong place for the insertion point. However, I started this thread to figure out why I was getting a missing dependency error. Even after implementing the solution below, I still got the same error message, about missing a dependency.

解释是这样的:

由于某种原因,插入点文件或它需要的任何文件的任何问题都会返回一个错误,指出缺少插入点文件模块.

For some reason, any problem with the insertion point file, or any file that it requires will throw back an error saying that the insertion point file module is missing.

我在一个文件中有一些没有为 OpenShift 正确配置的变量,OpenShift 日志中包含的最有用的错误消息是缺少根模块.

I had some variable in one file that wasn't configured right for OpenShift and the most helpful error message contained in the OpenShift logs was that the root module was missing.

结论缺少依赖项"错误可能只是意味着您在整个项目中的某个地方存在某种逻辑错误.[/解决方案]

Conclusion "missing dependency" errors might just mean that you have some kind of logic error, somewhere in your entire project. [/Solution]

推荐答案

您正在使用 "scripts" 属性,但在 OpenShift 上您确实应该使用 "main"反而.这是因为 OpenShift 使用 node-supervisor 来启动和管理您的 Node.js 应用程序.看起来如果你不提供 "main" 它默认为 server.js

You are using the "scripts" property, but on OpenShift you really should use "main" instead. This is because OpenShift uses node-supervisor to start and manage your Node.js application. It looks like if you don't supply a "main" it defaults to server.js

这篇关于如何找出 Node.js 项目中缺少的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆