Last Updated: February 25, 2016
·
4.562K
· tanathos

Intellisense on modular js application (RequireJS + VS)

Working in a huge modular project in the last months, I really missed a proper JS intellisense for RequireJS modules.

We have A LOT of modules and functions defined elsewhere and to write code without the support from the IDE became a real pain.

So I made some searches and I found out that the support for RequireJS modules intellisense WAS ALREADY THERE, in just few steps you can transform the coding of an AMD application from a nightmare in a real pleasure.

First of all you need to get the module developed by James Burke and place right next to require.js in your project.

Then be sure to add the requirejs-intellisense.js to your _references.js file.

/// <reference path="requirejs-intellisense.js" />

By default VS will search for this file in the ~/Scripts/ folder, if your project has a different folder's structure be sure to add your path in VS settings:

Picture

Now lets suppose to have a basic RequireJS applcation: a main.js file with a dependency to an helper utility file, like ~/scripts/helper/util.js.

main.js

require(["helper/util"], function (util) {

});

The util.js could be a module implemented with reveal pattern, you can now use the standard comments notation of VS to annotate functions and fields:

helper/util.js

define("helper/util", [], function () {
    var _awesomeUtilFunction = function (param1) {
        /// <summary>My awesome function description</summary>
        /// <param name="param1" type="String">Useless parameter</param>

        console.log("not so awesome, in the end");
    };

    return {
        awesomeUtilFunction: _awesomeUtilFunction,

        /// <field name='customValue' type='Number'>The answer.</field>
        customValue: 42
    };
});

Now, magically, you have intellisense support in the main.js: as you write util and dot, you can see functions and properties exposed by the util module, with your annotations.

Picture

Remember to hit CTRL + SHIFT + J to refresh the references for the intellisense, as you change your modules.

2 Responses
Add your response

hi, could you provide an example solution where intellisense works with RequireJS??
could not get this work with my VS..

thanks!!!
'-cenk

over 1 year ago ·

Hi, sorry for the late reply.
I see the same issue now both on the demo project I used for this tip and on my real projects.
I suspect something changed on VS with the Update 2 or on the WebEssentials extension (which works also on JS intellisense) so that the requirejs-intellisense.js is no more working properly.
Give me some time to investigate on this, I'm also interested to let it work again, I'll update here if I find something!
Thanks!

over 1 year ago ·