IntelliJ/WebStorm node.js 使用(自定义)所需模块完成代码不工作/未解析的功能?

IntelliJ/WebStorm node.js code completion with (custom) required modules not working / unresolved function?

例如,如果我有这个代码 (posts.js)

var mongoose = require('mongoose');

var postsSchema = mongoose.Schema({
    _id: Number,
    title: String,
    description: String,
    comments: [{ type: String, minlength: 1, required: true }]
});    
var Post = mongoose.model('Posts', postsSchema);    
module.exports = Post;

并将在某处使用此模型,例如:

var Post = require('../models/posts');
Post.{strg+space}

它会工作,但我不能使用代码完成,如果我调用函数,我会在 IntelliJ 编辑器中看到 "unresolved function" 错误。

如果我将导出更改为:

module.exports = {Post:Post};    

exports.Post = Post;

并导入模型:

var Post = require('../models/posts').Post;

一切似乎都很好。

这是一个错误还是我做错了什么?

IDE默认不维护用户编写的js文件中的符号。您需要将它们显式添加到项目中才能在代码完成中使用。

您可以通过转到设置 -> 语言和框架 -> Javascript -> 库来添加文件或库。

然后单击“添加”并使用 + 按钮添加您的文件。 它为您提供了一个选项,甚至可以添加整个目录。

这是一个已知问题,请关注WEB-17099更新