【问题标题】:Error: Cannot find module 'http-errors' on local npm错误:在本地 npm 上找不到模块“http-errors”
【发布时间】:2018-11-19 20:51:41
【问题描述】:

我在理解节点如何解析本地 node_modules 文件夹中的模块时遇到问题。我的应用程序使用了一个名为http-errors 的模块,它是node_modules 中的一个依赖项。我使用了一个Powershell命令来确认它

❯ npm ls --prod | Select-String -Pattern "http-errors"

| | +-- http-errors@1.6.3
| | | +-- http-errors@1.6.2
| | +-- http-errors@1.6.3

为什么找不到http-errors模块还是报错?

添加:这是我需要http-errors的地方

const createError = require('http-errors');

function catchNotFound(_req, _res, next) {
  next(createError(404));
}

【问题讨论】:

  • 您能分享一下您在 js 中使用http-errors 的位置吗?

标签: javascript node.js npm


【解决方案1】:

我弄清楚了 node.js 如何解析模块。打开 REPL 会话并输入

❯ node
> module.paths
[ 'C:\\Users\\truong\\Projects\\DDAC\\repl\\node_modules',
  'C:\\Users\\truong\\Projects\\DDAC\\node_modules',
  'C:\\Users\\truong\\Projects\\node_modules',
  'C:\\Users\\truong\\node_modules',
  'C:\\Users\\node_modules',
  'C:\\node_modules',
  'C:\\Users\\truong\\.node_modules',
  'C:\\Users\\truong\\.node_libraries',
  'C:\\Program Files\\nodejs\\lib\\node' ]
> require('http-errors')
Error: Cannot find module 'http-errors'
    at Function.Module._resolveFilename (module.js:536:15)
>

Node.js 只能解析这些路径中的模块。即使 npm 也有这种依赖关系,但是如果没有与模块名称完全相同的目录名称,则无法解析 require。在this article 阅读更多内容。

【讨论】:

    猜你喜欢
    • 2020-11-08
    • 2018-07-05
    • 1970-01-01
    • 2014-03-22
    • 2018-08-05
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多