简体   繁体   English

确定Node.js中已加载模块的路径

[英]Determining Paths for Loaded Modules in Node.js

In Node.js, is there any way to determine where on the filesystem a module was loaded from? 在Node.js中,有没有办法确定从哪个文件系统加载模块?

I do NOT mean, what directory context Node.js is executing in--which you can determine with process.cwd() . 不是说,Node.js正在执行什么目录上下文 - 您可以使用process.cwd()来确定。 I want to know something specific about whatever module is in memory. 我想知道关于内存中任何模块的具体内容。

For instance, in Python I can do the following... 例如,在Python中我可以执行以下操作...

>>> 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. 这显示了os模块来自文件系统的位置。 Is there anyway to do something similar in Node.js? 无论如何在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. 注意:当我问我之前的问题时,我试图提出这个问题,但是我说的很差,最后得到了一个不同问题的答案。

你应该能够使用require.resolve('module_name')

也许你可以使用require.resolve(...)函数来获得你想要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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