In node.JS, how can I get the path to the module that I loaded through require, this is * not * mine (i.e. in some node_module)

I need a module that was installed via npm. I want to access the .js file subordinate to this module (so I can subclass the Constructor method in it). I can’t (well, don’t want to) change the code of the module, so there is no place to retrieve its __dirname.

I know the following question, but it's about getting a path to a module that has control over the code (hence __dirname is the solution): In Node.js, how can I specify the path to this module?

~~~

It would be even better to get information about the loaded module module

+45
Apr 11 2018-12-12T00:
source share
3 answers

If I understand your question correctly, you should use require.resolve () :

Use the require () internal device to find the location of the module, but instead of loading the module, simply return the resolved file name.

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

+69
Apr 12 2018-12-12T00:
source share

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

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

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

+2
Apr 11 2018-12-12T00:
source share

According to @anatoliy's solution, on MacOS X I found search paths that perform

 require('module')._resolveLookupPaths('myModule') 

so i get allowed search paths

 [ 'myModule', [ '/Users/admin/.node_modules', '/Users/admin/.node_libraries', '/usr/local/lib/node' ] ] 

whereas

 require('module')._resolveFilename('myModule') 

won't allow the module that I was looking for anyway, actually the crazy thing is that _load won't solve the module:

 > require('module')._load('myModule') Error: Cannot find module 'myModule' at Function.Module._resolveFilename (module.js:440:15) at Function.Module._load (module.js:388:25) at repl:1:19 at sigintHandlersWrap (vm.js:32:31) at sigintHandlersWrap (vm.js:96:12) at ContextifyScript.Script.runInContext (vm.js:31:12) at REPLServer.defaultEval (repl.js:308:29) at bound (domain.js:280:14) at REPLServer.runBound [as eval] (domain.js:293:12) at REPLServer.<anonymous> (repl.js:489:10) 

and require will be:

 > require('myModule') 

but i don't have this module in

 myProject/node_modules/ myProject/node_modules/@scope/ /usr/local/lib/node_modules/ /usr/local/lib/node_modules/@scope /usr/local/lib/node_modules/npm/node_modules/ /usr/local/lib/node_modules/npm/node_modules/@scope $HOME/.npm/ $HOME/.npm/@scope/ 

so where is this module ???

First I had to do $ sudo /usr/libexec/locate.updatedb Then after some coffee I made locate myModule or better locate myModule/someFile.js

et voilĂ , it turns out that it was in the parent folder of my project, that is, outside the project root folder:

 $pwd /Users/admin/Projects/Node/myProject $ ls ../../node_modules/myModule/ 

therefore, you cannot avoid rm -rf ../../node_modules/myModule/ and the new npm install .

I can argue that no one instructed npm scan my computer in search of modules in a place other than the root folder of the project where it was supposed to run or in the default search path.

+1
Aug 04 '16 at 10:47
source share



All Articles