简体   繁体   English

在WebStorm中为通过npm安装的node.js模块启用自动完成功能

[英]Enabling autocompletion in WebStorm for node.js modules installed via npm

I am using WebStorm 5.0.4 to develop a node.js app. 我正在使用WebStorm 5.0.4开发node.js应用程序。

I installed several node modules globally using 我使用全局安装了几个节点模块

npm install -g module-name
and linked them into the project using 并使用它们将它们链接到项目中
 npm link module-name npm link module-name 

Autocompletion does not work for any of the npm installed modules. 自动完成功能不适用于任何npm安装的模块。 It only works for core modules (eg http or path ). 它仅适用于核心模块(例如httppath )。

Furthermore, WebStorm gives a warning "Unresolved function or method xyz" for any of the functions I call from npm installed modules. 此外,WebStorm为我从npm安装的模块调用的任何函数发出警告“Unresolved function or method xyz”。

How do I make WebStorm autocomplete and generally become aware of the installed modules that I require ? 如何使WebStorm自动完成并通常了解我require的已安装模块?

I'm not sure if this accounts for your particular situation, however I ran into a similar issue in WebStorm 5.0.4, whereas anything that was required was not auto-completing. 我不确定这是否适用于您的特定情况,但是我在WebStorm 5.0.4中遇到了类似的问题,而所需的任何内容都不是自动完成的。 I was able to resolve this issue by going into my Project Settings, navigating to JavaScript | 我可以通过进入我的项目设置,导航到JavaScript来解决此问题 Libraries, checking "Node.js Core Modules" and "Node.js Globals", and clicking apply. 库,检查“Node.js核心模块”和“Node.js Globals”,然后单击“应用”。

As per the link I posted in the comments, you'll need to add your global npm directory using 根据我在评论中发布的链接,您需要使用添加全局npm目录
Preferences -> Directories 偏好 - >目录

You can find out where your global directory is by running: 您可以通过运行来查找全局目录的位置:

> npm ls -g

Source: http://youtrack.jetbrains.com/issue/WEB-1880 资料来源:http: //youtrack.jetbrains.com/issue/WEB-1880

My answer works only on WebStorm 7 onwards: 我的回答仅适用于WebStorm 7以后:

  1. Open the Settings dialog (File | Default Settings) and select JavaScript | 打开“设置”对话框(“文件”|“默认设置”)并选择“JavaScript” Node.js. Node.js的

    1. Specify the path to Node.js interpreter and the Node.js version will be determined automatically. 指定Node.js解释器的路径,Node.js版本将自动确定。

    2. Click Configure and then click the 'Download and Configure' button to download Node.js sources to the IntelliJ system local folder. 单击“配置”,然后单击“下载并配置”按钮将Node.js源下载到IntelliJ系统本地文件夹。 A JavaScript library named “Node.js v. Core Library” will be created from the extracted source files of core modules. 将从提取的核心模块源文件创建名为“Node.js v.Core Library”的JavaScript库。

    3. Finally define a usage scope for JavaScript library just created. 最后为刚刚创建的JavaScript库定义一个使用范围。 By default the whole project will be added to the usage scope. 默认情况下,整个项目将添加到使用范围。 If that does not work for you, you can tune the usage scope by clicking the 'Edit usage scope' hyperlink. 如果这对您不起作用,您可以通过单击“编辑使用范围”超链接来调整使用范围。

If you need more information, read up on JetBrain's Official Blog on Attaching the sources of Node.js core modules , that should ideally solve your problem, it actually did for me at least. 如果您需要更多信息,请阅读JetBrain关于附加Node.js核心模块源代码的官方博客,理想情况下应该解决您的问题,它实际上至少对我有用。

This happens when you declare multiple modules with a comma such as: 当您使用逗号声明多个模块时会发生这种情况,例如:

var sys = require("sys"),
    http= require("http");

in the example above sys will only have exports as an auto complete option where as http will work fine. 在上面的例子中,sys只会将导出作为自动完成选项,因为http可以正常工作。 If you do: 如果你这样做:

var sys = require("sys");
var http= require("http");

both sys and http will work fine. sys和http都可以正常工作。

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

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