How do you specify NODE_PATH for local module autocompletion?

Hi,

My question is rather simple. By default, it looks like WebStorm searches uses the `node_modules` directory to resolve packages that are imported with `require()`. This lets me Crtl+Click the module name to jump to the module file, which is awesome!

However, Node supports a NODE_PATH environment variable, which essentially tells Node to look in another directory for modules, too. That way, instead of writing something like `require('./../../lib/models/user')`, I can set my NODE_PATH to my lib directory and just write `require('lib/models/user')`, from anywhere.

Unfortunately, WebStorm then tells me that the module is not installed, and won't let me Ctrl+Click through to it.

Is there any way to tell WebStorm what the NODE_PATH variable is, so it will look in that directory, too?

Thanks so much!

3 comments
Comment actions Permalink

So, I found a workaround.

Reading this explanation, it seems that Node searches any and all node_modules directories from the current directory up to the root.

Therefore, just creating a node_modules directory and placing local modules in it not only works in Node, without requiring NODE_PATH to be set, but it also works in WebStorm.

So, the project structure might look like this:

project_folder
     node_modules
     server
         node_modules
         module
             mini_module
                  index.js
             index.js
     server.js
     package.json

server.js would have to require server like so:

     `require('./server')`

but that file could then require mini_module like so:

     `require('module/mini_module')`

So, not exactly what I was looking for, but workable. And, the benefit is that I don't have to fiddle around with any NODE_PATH funny business, which isn't really recommended, anyway.

0
Comment actions Permalink

So, it turns out there is a way to do this: https://youtrack.jetbrains.com/issue/WEB-19476

Elana, from JetBrains support, sent me the link. Thanks, Elena!

1
Comment actions Permalink

Just bumped in the same issue. Joshua Hutt, thank you for the information, it was very helpful, I found the solution in your link (early comment).

 

  • right-click the directory ( for example src )
  • select "Mark Directory As" -> "Resource Root"

After this actions "jump" to files works properly.

 

 

0

Please sign in to leave a comment.