node.js:为什么 NODE_DEBUG=1 不起作用? (尝试调试 require() 错误)

发布于 2024-10-01 19:48:42 字数 2356 浏览 4 评论 0原文

我有一个像这样的目录结构:

project
  lib
    paperboy
    redis-client
    node-cookie
  srv
    main.js
  ...

我从项目目录启动 main.js:

$ node srv/main.js

在 main.js 中,我可以这样做:

  paperboy = require('./lib/paperboy');

但是,这失败了:

  redis = require('./lib/redis-client');

同样,如果我在“project”目录中启动交互式节点,我可以 require paperboy ,但不是 redis 客户端。我得到的错误是:

> require('./lib/redis-client')
Error: Cannot find module './lib/redis-client'
    at resolveModuleFilename (node.js:265:13)
    at loadModule (node.js:231:20)
    at require (node.js:291:14)
...

查看resolveModuleFilename()的源代码,它尝试打印调试字符串,但我没有看到:

debug("looking for " + JSON.stringify(id) + " in " + JSON.stringify(paths));

我尝试通过导出NODE_DEBUG = 1启用此功能,但我仍然没有看到此调试尝试要求时打印。

我在尝试打印此调试时做错了什么? 其次,为什么 paperboy 加载正常,但找不到 redis-client ?

附加信息:以下是“lib”目录中的完整文件/目录列表:

lib
lib/cookie-node
lib/cookie-node/package.json
lib/cookie-node/LICENSE.txt
lib/cookie-node/README.markdown
lib/cookie-node/example
lib/cookie-node/example/ex1.js
lib/cookie-node/index.js
lib/redis-client
lib/redis-client/package.json
lib/redis-client/TODO.md
lib/redis-client/examples
lib/redis-client/examples/redis-version.js
lib/redis-client/examples/using-kiwi.js
lib/redis-client/examples/subscriber.js
lib/redis-client/examples/publisher.js
lib/redis-client/examples/.redis-version.js.swp
lib/redis-client/examples/README.md
lib/redis-client/seed.yml
lib/redis-client/LICENSE
lib/redis-client/test
lib/redis-client/test/test_throw_from_callback.js
lib/redis-client/test/test_shutdown_reconnect.js
lib/redis-client/test/test.js
lib/redis-client/test/sample.png
lib/redis-client/.gitignore
lib/redis-client/lib
lib/redis-client/lib/redis-client.js
lib/redis-client/README.md
lib/paperboy
lib/paperboy/package.json
lib/paperboy/seed.yml
lib/paperboy/LICENSE.txt
lib/paperboy/example
lib/paperboy/example/basic.js
lib/paperboy/example/webroot
lib/paperboy/example/webroot/img
lib/paperboy/example/webroot/img/paperboy.jpg
lib/paperboy/example/webroot/index.html
lib/paperboy/index.js
lib/paperboy/lib
lib/paperboy/lib/paperboy.js
lib/paperboy/README.md

lib 目录是从 github 解压的 .tar.gz 文件,并重新命名以匹配 package.json 文件中的模块名称。

I have a directory structure like so:

project
  lib
    paperboy
    redis-client
    node-cookie
  srv
    main.js
  ...

I start main.js from the project directory:

$ node srv/main.js

In main.js, I can do:

  paperboy = require('./lib/paperboy');

However, this fails:

  redis = require('./lib/redis-client');

Similarly, if I start interactive node in the "project" directory, I can require paperboy, but not redis-client. The error I get is:

> require('./lib/redis-client')
Error: Cannot find module './lib/redis-client'
    at resolveModuleFilename (node.js:265:13)
    at loadModule (node.js:231:20)
    at require (node.js:291:14)
...

Looking at the source for resolveModuleFilename(), it attempts to print a debug string, that I don't see:

debug("looking for " + JSON.stringify(id) + " in " + JSON.stringify(paths));

I have tried enabling this through export NODE_DEBUG=1, but I still don't see this debug print when trying to require.

What am I doing wrong in trying to get this debug to print?
And, second, why would paperboy load fine, but redis-client fail to be found?

Additional info: Here's the full file/directory list in the "lib" directory:

lib
lib/cookie-node
lib/cookie-node/package.json
lib/cookie-node/LICENSE.txt
lib/cookie-node/README.markdown
lib/cookie-node/example
lib/cookie-node/example/ex1.js
lib/cookie-node/index.js
lib/redis-client
lib/redis-client/package.json
lib/redis-client/TODO.md
lib/redis-client/examples
lib/redis-client/examples/redis-version.js
lib/redis-client/examples/using-kiwi.js
lib/redis-client/examples/subscriber.js
lib/redis-client/examples/publisher.js
lib/redis-client/examples/.redis-version.js.swp
lib/redis-client/examples/README.md
lib/redis-client/seed.yml
lib/redis-client/LICENSE
lib/redis-client/test
lib/redis-client/test/test_throw_from_callback.js
lib/redis-client/test/test_shutdown_reconnect.js
lib/redis-client/test/test.js
lib/redis-client/test/sample.png
lib/redis-client/.gitignore
lib/redis-client/lib
lib/redis-client/lib/redis-client.js
lib/redis-client/README.md
lib/paperboy
lib/paperboy/package.json
lib/paperboy/seed.yml
lib/paperboy/LICENSE.txt
lib/paperboy/example
lib/paperboy/example/basic.js
lib/paperboy/example/webroot
lib/paperboy/example/webroot/img
lib/paperboy/example/webroot/img/paperboy.jpg
lib/paperboy/example/webroot/index.html
lib/paperboy/index.js
lib/paperboy/lib
lib/paperboy/lib/paperboy.js
lib/paperboy/README.md

The lib directories are unpacked .tar.gz files from github, re-named to match the module name from the package.json files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国际总奸 2024-10-08 19:48:42

您想要导出 NODE_DEBUG=module,而不是 =1

You want export NODE_DEBUG=module, not =1

最笨的告白 2024-10-08 19:48:42
  1. Node.js 查找与脚本位置相关的所需文件,因此您应该使用

    paperboy = require('../lib/paperboy');

    在 srv/mail.js 中。

  2. 据我所知,您必须使用 --debug 选项配置 Node.js,然后使其使用任何调试功能。

  1. Node.js looks for requirable files relative to script location, so you should use

    paperboy = require('../lib/paperboy');

    in srv/mail.js.

  2. You must configere node.js with --debug option and then make it to use any debug features, as I know.

万劫不复 2024-10-08 19:48:42

这不起作用的原因是 Node.js 会自动包含一个名为 index.js 的脚本

Node Cookie 和 Paperboy 看起来像这样:

lib/cookie-node/index.js
lib/paperboy/index.js

Redis 客户端看起来像这样:

lib/redis-client/lib/redis-client.js

你需要更改你的要求:

var redis = require('./lib/redis-client/lib/redis-client');

本质上,节点会像这样查找需要的文件:

var redis = require('./lib/redis-client');

   ./lib/redis-client.js
   ./lib/redis-client/index.js // (if redis-client is a directory).

由于没有 index.js 文件,节点无法继续在 ./lib/redis-client/ 的 lib 目录中查找并且不会包含该文件,因为它不知道该文件的名称或位置。

The reason this isn't working is because Node.js will automatically include a script if it has the name index.js

Node Cookie and Paperboy look like this:

lib/cookie-node/index.js
lib/paperboy/index.js

Redis Client looks like this:

lib/redis-client/lib/redis-client.js

You need to change your require to:

var redis = require('./lib/redis-client/lib/redis-client');

Essentially, node looks for require files like so:

var redis = require('./lib/redis-client');

   ./lib/redis-client.js
   ./lib/redis-client/index.js // (if redis-client is a directory).

As there is no index.js file, node is unable to carry on looking in the lib directory of ./lib/redis-client/ and will not include the file, as it does not know what it is called or where it should be located.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文