node.js:为什么 NODE_DEBUG=1 不起作用? (尝试调试 require() 错误)
我有一个像这样的目录结构:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要导出
NODE_DEBUG=module
,而不是=1
You want export
NODE_DEBUG=module
, not=1
Node.js 查找与脚本位置相关的所需文件,因此您应该使用
paperboy = require('../lib/paperboy');
在 srv/mail.js 中。
据我所知,您必须使用 --debug 选项配置 Node.js,然后使其使用任何调试功能。
Node.js looks for requirable files relative to script location, so you should use
paperboy = require('../lib/paperboy');
in srv/mail.js.
You must configere node.js with --debug option and then make it to use any debug features, as I know.
这不起作用的原因是 Node.js 会自动包含一个名为
index.js
的脚本Node Cookie 和 Paperboy 看起来像这样:
Redis 客户端看起来像这样:
你需要更改你的要求:
本质上,节点会像这样查找需要的文件:
由于没有
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:
Redis Client looks like this:
You need to change your require to:
Essentially, node looks for require files like so:
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.