简体   繁体   English

Intellij IDEA解析本地require()路径[node.js]

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

I'm trying to avoid relative require() calls in my express setup. 我试图在我的快速设置中避免相对的require()调用。 I'd also like to avoid placing my code in the node_modules folder. 我也想避免将我的代码放在node_modules文件夹中。 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 . 这些解决方案中的任何一个都可以很好地用于使用nodenpm执行代码。 However, I'm trying to find a solution that will also be supported by Intellij IDEA's code resolver, ie trying to make sure "go to declaration" and autocomplete hinting works. 但是,我试图找到Intellij IDEA的代码解析器也将支持的解决方案,即试图确保“声明”和自动完成提示功能正常。

I've tried the following 我尝试了以下

  • Setting NODE_PATH in the run configuration. 在运行配置中设置NODE_PATH
  • Using a global prefix, ie require( global.__base + "mylib") . 使用全局前缀,即require( global.__base + "mylib")
  • Adding a symlinked folder to node_modules/ . node_modules/链接的文件夹添加到node_modules/

Adding a symlink from a lib/ folder to node_modules/lib/ does work, but comes with two caveats: 将符号链接从lib/文件夹添加到node_modules/lib/确实可以,但是有两个警告:

  • Changes to the source files aren't picked up automatically, so I have to manually "synchronize" node_modules/lib , and 不会自动获取对源文件的更改,因此我必须手动“同步” node_modules/lib ,并且
  • When "going to declaration", IntelliJ (of course) opens node_modules/lib/mylib instead of lib/mylib . 当“去声明”时,IntelliJ(当然)将打开node_modules/lib/mylib而不是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. 而不是要求本地路径的其他方法(毕竟所有这些方法都可以与node一起使用),我很乐意向IDEA暗示它应该在lib/文件夹中搜索源。

So, I realised that if you add a library through Project Structure > Libraries , it won't actually be enabled. 因此,我意识到,如果通过“ Project Structure > Libraries添加Project Structure > Libraries ,则实际上不会启用该Project Structure > Libraries

Instead, go to Preferences > Languages & Frameworks > Javascript > Libraries and add a new library. 相反,请转到Preferences > Languages & Frameworks > Javascript > Libraries然后添加一个新的库。 Set the framework type to node_modules , Visibility to Project and add your lib folder. 将框架类型设置为node_modules ,对Project可见性并添加lib文件夹。

After adding it, make sure the Enabled checkbox is checked. 添加后,请确保已选中“ Enabled复选框。

That's it, Intellij can now resolve your require('mylib') paths. 就是这样,Intellij现在可以解析您的require('mylib')路径。

Use whatever method from the gist mentioned in the question to actually get node to resolve the paths. 使用问题中提到的要点中的任何方法来实际获取node来解析路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM