简体   繁体   中英

Is there a way to know where is require/resolve tryting to find modules in Node.js?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM