Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • Setting NODE_PATH in the run configuration.
  • Using a global prefix, i.e. require( global.__base + "mylib").
  • Adding a symlinked folder to node_modules/.

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

  • Changes to the source files aren't picked up automatically, so I have to manually "synchronize" node_modules/lib, and
  • When "going to declaration", IntelliJ (of course) opens node_modules/lib/mylib instead of lib/mylib. This can lead to confusion as the actual file and the symlinked file can be open in separate windows.

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.

like image 780
jkgeyti Avatar asked Apr 07 '26 12:04

jkgeyti


1 Answers

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.

like image 82
jkgeyti Avatar answered Apr 09 '26 04:04

jkgeyti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!