var p = require.resolve('sys');
require('sys').puts(p);
Outputs on my system:
/usr/local/lib/node/libraries/sys.js
I know I would find this very useful and a few other people have
expressed interest in this functionality (I can't find the thread
right now). I could write my own module to do it, but I would
manually have to traverse the require.paths array looking for a match.
Sense Node already does this it seems useful to expose the
functionality to other people.
What do you think? Useful?
Narwhal provides a require.loader object with this API:
* resolve(id, baseId) : top-level ID
* find(id) : path String
* fetch(id) : content String
* paths Array
* extensions Array, e.g., [".js", ""]
Irrelevant details elided. This is one of the proposals for
interoperability on this:
http://wiki.commonjs.org/wiki/Modules/Loaders/B
Kris Kowal
This would be really useful for me, I've already created an issue in
github: http://github.com/ry/node/issues/#issue/68 - I was asking for
a different implementation, but this would be fine (and has the
benefit of not messing with the exports).
--
Jonas Pfenniger (zimbatm) <jo...@pfenniger.name>
Well my exact use case is this:
I working on a web framework that allows people to piece together
different Node modules to create their website. These modules are
called "apps". You can see it here: http://github.com/obt/bomberjs
Basically, each of the modules can add a list of arbitrary tasks to
the start up script. So, if the module's structure is something like:
module/
index.js
tasks/
task1.js
task2.js
task3.js
....
The only way I can currently access the tasks is by having the module
export a list of the tasks it has. Which is not very DRY. So, what
I'd like to be able do is
import the module, figure out its location, use fs.readdir to find all
the files in module_path/tasks and then import those one by one. In
order to do that I need to know where on disk the module is.
Another use case is if a bunch of modules have test directories and I
want to run through all of the module's test directories and run all
the test files (maybe for
http://github.com/bentomas/node-async-testing). Once again I need to
know the location of that test directory in order get the list of test
files.
Yes, it is possible for the modules to export __filename. But what if
I am inspecting a module that didn't for see me wanting to be able to
look at the other files in the directory? The only way is to loop
through require.paths array and manually find the one that was
required. And node has code that does this built-in. So, I'd like to
not have to redo code that is already built-in.
Finally, as we install more modules in ~/.node_libraries and any other
directories in $NODE_PATH it is useful to be able to know which file
was actually required, for debugging or what not.
At least that's what I plan on using this for.
Also, see the issue the caolan made on GitHub. It has some much
better use cases than mine: http://github.com/ry/node/issues/#issue/68
Ok, I see where you're coming from. I understand that adding
require.resolve is a pragmatic way to solve your problem, and not
necessarily bad.
What would you think of adding a "module-info" module for such
introspection stuff ?
var mi = require('module-info');
mi.resolve(exports) //=> /usr/local/.../mystuff.js