In node.JS how can I get the path of a module I have loaded via require that is *not* mine (i.e. in some node_module)

前端 未结 7 1710
清歌不尽
清歌不尽 2020-12-02 15:16

I require a module that was installed via npm. I want to access a .js file subordinate to that module (so I can subclass a Constructor method in it). I can\'t (well, don\'t

7条回答
  •  -上瘾入骨i
    2020-12-02 15:41

    I hope I correctly understand your needs: to get entry point file of some module. Let's say you want to get entry point of jugglingdb module:

    node
    > require('module')._resolveFilename('jugglingdb')
    '/usr/local/lib/node_modules/jugglingdb/index.js'
    

    As you can see this is not "official" way to get this kind of information about module, so behavior of this function may change from version to version. I've found it in node source: https://github.com/joyent/node/blob/master/lib/module.js#L280

提交回复
热议问题