Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need workaround to make WebStorm's autocompletion capable to resolve third party module methods

Almost for all third-party modules WebStorm's autocomplition cannot resolve methods/fields. Under autocompletion I mean also all intellisense-like features. For example:

var async = require('async');
async.series() //WebStorm's tooltip says: Unresolved function or method series()

At the same time it resolves

async.exports.series(). 

But this leads to runtime error:

TypeError: Cannot call method 'series' of undefined

For my own modules I've found workaround. If I do in the module:

var myModule = module.exports;
myModule.someMethod = function(){
...
}

Then autocomplition for someMethod works fine.

Regarding all of above I have a bunch of questions.
1. Why the ide fails to resolve async.series()?
2. Why async.exports.series() leads to runtime error?
3. How to make autocomplition work?

WebStorm 5.0.4.

like image 605
alehro Avatar asked Jan 14 '13 11:01

alehro


3 Answers

Go to Settings -> JavaScript -> Libraries -> Check "Node.js Globals"

like image 106
Vanuan Avatar answered Nov 18 '22 03:11

Vanuan


New WebStorm v7 has ability to define Typescript community stubs for popular modules. This partially solves the problem with autocompletion and IDE warnings.

For less popular modules I prefer to use this ugly cheat:

//noinspection ConstantConditionalExpressionJS,JSPotentiallyInvalidConstructorUsage
var async = false ? new require('async') : require('async');

However, this doesn't solves the problem, when property been attached to module by some algorithm (for example iterating filesystem). For small and unpopular modules it is a rare case.

BTW, async has already typescript stub in [email protected]:borisyankov/DefinitelyTyped.git repository.

like image 2
Dao Avatar answered Nov 18 '22 03:11

Dao


Use new as follows:

var async = new require('async');
like image 12
alehro Avatar answered Nov 18 '22 04:11

alehro