[英]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
. 这些解决方案中的任何一个都可以很好地用于使用
node
或npm
执行代码。 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 我尝试了以下
NODE_PATH
in the run configuration. NODE_PATH
。 require( global.__base + "mylib")
. require( global.__base + "mylib")
。 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/
确实可以,但是有两个警告:
node_modules/lib
, and node_modules/lib
,并且 node_modules/lib/mylib
instead of lib/mylib
. 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.