How to check where in the project a module is required?

Hello there, I'm doing some test workflows with WebStorm and am planning on fully switching to it. So, I want to have a quick and effective way to find where a file is being required (using Node's require) without having to search for it's filename.

Assume I have a file like this:

// file test.js

var x = {
a: function(){console.log("hello")},
b: function(){console.log("world")}
}

module.exports = x;

And then, in another file I require it.

// file usingTest.js

var y = require('./test.js');

y.a();

Now, while at test.js, I can do a CTRL+G command while selecting the 'a' property inside x, and it will show me it's being used on the file above. That's great. But for my surprise, I can't do that with the x variable, it won't show the var y = require('./test.js') line, even though I get method completion for y.

So, is this a bug? Or maybe there's a smarter way to do this? I'm talking about javascript only by the way, not typescript.

Thanks in advance!

EDIT: Upon further testing, I just realized ctrl+g is more like a generic search for the keyword, so in my previous example it's searching for any object with the property 'a'.

It seems it's not exactly what I wanted, I was thinking at something like eclipse's References in Workspaces (ctrl+shift+g). So I'm adding another question here: if the IDE is smart enough to find the correct declaration/implementation using Go To Declaration, isn't there a way to find it's references (without searching only for the property name, since it would show properties/methods with the same name) ?

3 comments
Comment actions Permalink

You can search for test.js usages (right-click the file test.js, Find usages). WebStorm can find usages of exported module properties, but require(file_name) call is not treated as module usage

0
Comment actions Permalink

Thanks, this works for me. Just out of curiosity, why isn't require(file_name) treated as module usage? I'm assuming only ES6 modules are being treated as such?

0
Comment actions Permalink

Only ES6 named imports are treated this way

0

Please sign in to leave a comment.