Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

Tags:

node.js

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 want to) modify the module's code, so don't have a place to extract its __dirname.

I am aware of the following question, but it is about getting the path of a module that one has code control over (hence, __dirname is the solution): In Node.js how can I tell the path of `this` module?

~~~

Even better would be to get the module's loaded module info

like image 953
Zhami Avatar asked Apr 11 '12 17:04

Zhami


People also ask

How modules are loaded in node JS?

These modules can be loaded into the program by using the require function. Syntax: var module = require('module_name'); The require() function will return a JavaScript type depending on what the particular module returns.

How do I specify a path in node JS?

js: var fs = require('fs') var newPath = "E:\\Thevan"; var oldPath = "E:\\Thevan\\Docs\\something. mp4"; exports. uploadFile = function (req, res) { fs. readFile(oldPath, function(err, data) { fs.

Which command is used to import the required modules in node JS?

Importing from core modules: These modules are inbuilt in Node. js and can be imported as: const var = require('fs');


1 Answers

If I correctly understand your question, you should use 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.

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

like image 192
Linus Thiel Avatar answered Sep 17 '22 14:09

Linus Thiel