Jon Watte
Jon Watte

Reputation: 7198

node.js: Why does NODE_DEBUG=1 not work? (trying to debug a require() error)

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.

Upvotes: 1

Views: 2614

Answers (3)

Martin Cleaver
Martin Cleaver

Reputation: 1914

You want export NODE_DEBUG=module, not =1

Upvotes: 10

Robin Duckett
Robin Duckett

Reputation: 2104

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.

Upvotes: 2

Sannis
Sannis

Reputation: 250

  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.

Upvotes: 3

Related Questions