Reputation: 3367
I want to be able to know what path node's require will resolve to without running a require.
For example
var myVar = require('./some_module')
Internally node.js will look through its PATH, figure out whether it's a directory, a js file, a json file, etc., and then load the file.
Is there a way to just as for the full resolved path without actually loading the module?
The output I'm looking for would be something like
/Users/myUser/awesomeProject/some_module.js
The important part being the .js extension, since node would have had to fully resolve the path.
note: require('path').resolve ...
does not solve this issue. What if some_module is actually a directory with a package.json inside of it, etc.
Upvotes: 3
Views: 604
Reputation: 3367
Turns out node has a way to do this! It's just not easy to find in their documentation. Calling
require.resolve('./some_module')
gives me exactly what I'm looking for
Here's the only reference I've found in the docs: https://nodejs.org/api/modules.html#modules_all_together
Upvotes: 4