找不到模块:无法解析“[...]\node_modules\follow-redirects”中的“调试”

Daw*_*ach 9 reactjs next.js

在构建应用程序时,我遇到来自 3rd party lib 的警告,如下所示:

warn  - ./node_modules/follow-redirects/debug.js
Module not found: Can't resolve 'debug' in 'G:\Workspace\node\MERN\client\node_modules\follow-redirects'
Did you mean './debug'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules).    
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.
Run Code Online (Sandbox Code Playgroud)

我的堆栈:

  "dependencies": {
    "@fortawesome/fontawesome-free": "^6.2.1",
    "axios": "^1.2.2",
    "bootstrap": "^5.2.3",
    "mdb-ui-kit": "^6.0.1",
    "next": "^13.1.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-toastify": "^9.1.1"
  }
Run Code Online (Sandbox Code Playgroud)

follow-redirects 中 debug.js 的内容:

var debug;

module.exports = function () {
  if (!debug) {
    try {
      /* eslint global-require: off */
      debug = require("debug")("follow-redirects");
    }
    catch (error) { /* */ }
    if (typeof debug !== "function") {
      debug = function () { /* */ };
    }
  }
  debug.apply(null, arguments);
};
Run Code Online (Sandbox Code Playgroud)

并在index.js文件中导入:

var url = require("url");
var URL = url.URL;
var http = require("http");
var https = require("https");
var Writable = require("stream").Writable;
var assert = require("assert");
var debug = require("./debug");
Run Code Online (Sandbox Code Playgroud)

我检查了他们的回购协议和问题,但显然没有人报告过它,所以我想知道这是否与我的特定设置有关?

我还尝试删除 node_modules 并通过 npm 重新安装所有依赖项,但没有成功。

Rea*_*lar 5

这是包的正常行为follow-redirects

debug.js文件执行以下操作:

  • 尝试加载debugnpm 包。
  • 捕获未找到它的错误。
  • 将一个空函数分配给该debug变量。

该代码旨在与debugNodeJs 的 npm 包一起使用,但这本来是可选的。

请参阅: https: //www.npmjs.com/package/debug

问题:

  • debug在代码周围重复使用这个词是令人困惑的。有 3 个不同的调试引用:debug.js中的文件follow-redirectsdebug该文件中的模块变量以及debugnpm 包(未安装)。

列出follows-redirect作为debug可选对等依赖项:

  "peerDependenciesMeta": {
    "debug": {
      "optional": true
    }
  },
Run Code Online (Sandbox Code Playgroud)

您看到的警告:

Module not found: Can't resolve 'debug' in 'G:\Workspace\node\MERN\client\node_modules\follow-redirects'
Run Code Online (Sandbox Code Playgroud)

来自 NextJs,它被报告用于编译服务器端代码,因为 NodeJs 似乎无法解析包。

如何follow-redirects检查包的使用debug是一种代码味道。它应该是一个用户可配置的功能,而不是仅仅因为 npm 包作为对等依赖项安装,或者懒惰的 require() 并捕获错误而强制执行的功能。

您可以选择安装调试并使用修复警告的功能。

npm install debug
Run Code Online (Sandbox Code Playgroud)

或者,忽略该警告。这样做并没有什么坏处。


lyz*_*lyz 0

看起来这条线debug = require("debug")("follow-redirects");应该是 debug = require("./debug")("follow-redirects");