jkgeyti
jkgeyti

Reputation: 2404

Intellij IDEA resolve local require() path [ node.js ]

I'm trying to avoid relative require() calls in my express setup. I'd also like to avoid placing my code in the node_modules folder. In short, I'm trying to implement any of the methods described in this gist.

Any of those solutions will work fine for executing code with node or npm. However, I'm trying to find a solution that will also be supported by Intellij IDEA's code resolver, i.e. trying to make sure "go to declaration" and autocomplete hinting works.

I've tried the following

Adding a symlink from a lib/ folder to node_modules/lib/ does work, but comes with two caveats:

Instead of a different way to require local paths (all these methods do work with node after all), I'd be happy with a way to hint to IDEA that it should search the lib/ folder for sources.

Upvotes: 3

Views: 1513

Answers (1)

jkgeyti
jkgeyti

Reputation: 2404

So, I realised that if you add a library through Project Structure > Libraries, it won't actually be enabled.

Instead, go to Preferences > Languages & Frameworks > Javascript > Libraries and add a new library. Set the framework type to node_modules, Visibility to Project and add your lib folder.

After adding it, make sure the Enabled checkbox is checked.

That's it, Intellij can now resolve your require('mylib') paths.

Use whatever method from the gist mentioned in the question to actually get node to resolve the paths.

Upvotes: 4

Related Questions