3

In Node.js, is there any way to determine where on the filesystem a module was loaded from?

I do NOT mean, what directory context Node.js is executing in--which you can determine with process.cwd(). I want to know something specific about whatever module is in memory.

For instance, in Python I can do the following...

>>> import os
>>> os.__file__
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.pyc'

Which shows me where on the filesystem the os module is from. Is there anyway to do something similar in Node.js?


NOTE: I was trying to ask this question when I asked my previous question, but I phrased it poorly and ended up getting an answer to a different question.
Community
  • 1
  • 1
Chris W.
  • 37,583
  • 36
  • 99
  • 136

2 Answers2

8

You should be able to use require.resolve('module_name')

rbginge
  • 906
  • 1
  • 6
  • 13
  • I think that is subtly different from my question in that it will tell you what file 'would be' loaded for a given module name, instead of allowing you to determine what file was loaded for a given module. But it certainly works for my purposes! Thanks. – Chris W. Mar 10 '11 at 22:01
0

Maybe you could use the require.resolve(...) function to get what you are looking for.

Jerome WAGNER
  • 21,986
  • 8
  • 62
  • 77