node.js - 如何获取我通过 *not* 我的 require 加载的模块的路径(即在某些 node_module 中)

标签 node.js

我需要一个通过 npm 安装的模块。我想访问从属于该模块的 .js 文件(因此我可以在其中子类化一个 Constructor 方法)。我不能(好吧,不想)修改模块的代码,所以没有地方可以提取它的 __dirname。

我知道以下问题,但它是关于获取具有代码控制权的模块的路径(因此,__dirname 是解决方案): In Node.js how can I tell the path of `this` module?

~~~

最好是获取模块的加载模块信息

最佳答案

如果我正确理解您的问题,您应该使用 require.resolve() :

Use the internal require() machinery to look up the location of a module, but rather than loading the module, just return the resolved filename.

示例:var pathToModule = require.resolve('module');

关于node.js - 如何获取我通过 *not* 我的 require 加载的模块的路径(即在某些 node_module 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10111163/

相关文章:

javascript - 如何在浏览器中读取 Connect Js cookie?

node.js - 在 Node.js 上使用非阻塞操作响应请求处理程序

javascript - 测试一个函数在 promise 解析中被调用

node.js - Node.js 中未定义 XSLTProcessor

javascript - 有没有办法像 Mongoose 一样在 Sequelize 时间戳中设置 'expires' 选项?

node.js - 下载文件后重定向 - Nodejs

javascript - 计算 JavaScript/NodeJS/Underscore 中的哈希值

node.js - 错误: Electron packaging with electron-packager

node.js - 如何访问运行在 Mesos 上的 Web 应用程序?

javascript - 安装和使用 React