I remember when I first heard about Node.js. JavaScript on the server? Why would anyone want to do that? I hadn’t yet experienced the expressive power and conciseness of JavaScript, but once I did, I was hooked. Suddenly I was very interested in Node.js and started looking for a reason to jump in and start playing with it. Well, I recently got that chance, and while I am enjoying it very much, the start of my experience has had its share of bumps in the road. I’ll share a few of the problems I had and their workarounds or solutions.
Install a version of Node with a working debugger
This might seem like an odd thing to say, but at the time of this writing, neither the LTS (v4.2.6) nor the Stable (v5.5.0) versions of Node have a debugger that can survive a call to util.inspect(). As noted in this issue, the last release that would not bring the debugger down with this very common function call was 4.2.4. This bug is very easy to reproduce and at first I blamed Node Tools for Visual Studio, but node-inspector was just as impacted. In my case, I first encountered it by trying to access an Azure Notification Hub through the azure NPM module. Downgrading from 5.5.0 to 4.2.4 worked around this issue for me.
Install support for node-gyp
If you’re really new to Node like me, you might also be blissfully unaware of this rather crucial component of Node and NPM that is used to compile native modules on demand. The full requirements and installation instructions are located on the node-gyp page. For me, the main parts of that meant installing Python 2.7 (as noted on the node-gyp page, Python 3.x is not supported) and installing C++ support in Visual Studio 2015 (I had only installed C# and VB.NET support initially). If you’re in a similar situation, you can prod Visual Studio into offering to install C++ components by selecting File | New Project, and in the New Project dialog, select Visual C++ in the left tree and choose an option from the center pane to install necessary components.
Work around Windows MAX_PATH issues
My recommended version of Node (4.2.4) comes out of the box with NPM 2.x. This is significant because up until 3.x, NPM would nest referenced modules within the directory of the parent module’s directory. For large modules with many dependencies, this strategy can quickly lead to paths that are longer than what Windows can handle (260 characters). The good news is that NPM changed this strategy starting with version 3.x, which comes by default with Node 5.x. However, as I said above, currently available 5.x versions of Node come with a debugger that croaks as soon as a module anywhere calls util.inspect. Fortunately, NPM is decoupled from Node, so after you’ve installed Node 4.2.4, just go to the command line and enter this command:
1 npm install npm -g
This will install the latest version of NPM globally. As of the latest version of Node Tools for Visual Studio (1.1), this will also cause NPM operations within Visual Studio to use the latest version of NPM. You may need to close and re-open Visual Studio to see this take effect; I did, but I didn’t try leaving it open while I installed the latest NPM.
Once you’ve performed these steps, you will have a version of Node that you can debug with NTVS and install packages that can build natively without exceeding the Windows filename limit. Happy Node-ing!