4

I'm working on a plugin system for a Node.js library and I'm having a little bit of trouble trying to figure out where the "require" and "require.resolve" functions are looking for packages when running some tests. I keep getting errors such as:

Cannot find module 'plugin-name' from "index.jS"

I know that typically Node.js would look for "node_modules" directories all the way up the hierarchy, but since I'm doing some mocking on my tests things are not that straightforward, so it would be really helpful if I could print some logs or debugging info of the paths Node.js is trying to find packages in.

Any ideas?

Thanks in advance.

1 Answers1

1

I think module.paths will probably be most useful. Running locally I get, what I think is a complete array of search paths, which includes the node_modules directories all the way up the tree as well as ~/.node_libraries, ~/.node_modules, and '/usr/local/lib/node'

The documentation simply say:

module.paths
The search paths for the module

There's a pretty good description on the algorithm here: https://nodejs.org/api/modules.html#modules_all_together

Mark
  • 90,562
  • 7
  • 108
  • 148