npm 3 will bring brighter future for Windows users

npm 3 will bring brighter future for Windows users

Around 40% of the npm users are using Windows. It is a big chunk but unfortunately, there has been long-running Windows specific issues that have caused frustration. One of the most significant issues has been deep dependency hierarchy that causes problems on a Windows machine. NPM 3 introduces a solution to this problem.

Long path names

If you have encountered the problem, then skip to the next section. Short description of the problem for those lucky persons who haven't encountered the error.

Let's say that you're doing all the development in a subfolder of the My Documents of your user account. It means that the path is something like this: C:\Users\Tx3\Documents\My Projects\Test

When you install a npm package, node_modules folder will be created, and whole path is then C:\Users\Tx3\Documents\My Projects\Test\node_modules

We have now spent 53 characters of max 260.

Wait, why 260 characters?

Microsoft's "About File Management" documentation has a very detailed description. Summary:

The problem is not the file system, such NTFS, but the Windows API.

Back to the problem.. About 200 characters left, which sounds a lot, but in reality it's not that much. If you do like this gentleman and install npm package that has deep dependency graph, you'll encounter this kind of node_modules structure:

\node_modules\grunt-bower-task\node_modules\bower\node_modules\update-notifier\node_modules\request\node_modules\form-data\node_modules\combined-stream\node_modules\delayed-stream\test\integration\test-delayed-stream-auto-pause.js

That block will occupy 231 characters and starts to cause problems. For example, deleting a folder uses Windows API. What I have done is that I move the folder to the C:\ root and then try the delete again.

To avoid all kind of path related issues, I have moved all my code close to the root folder of my drive, c:\GH for example.

npm 3 brings a real remedy to this problem. First we need to upgrade to it..

Upgrading to npm 3

Microsoft has made a tool that makes upgrading easier. npm-windows-upgrade is an npm package that lets you choose npm version to install.

Screenshot of an npm upgrade tool

Finally, you should get something like this:

Successful npm upgrage

After that, I cleared the npm cache using command npm cache clean and deleted node_modules folder.

Flat package structure

If you do npm install with a version 3 of npm you should see a progress bar. I guess it will be rendered more nicely in *nix based systems.

npm installing packages and displaying new progress bar

In the npm 2 era, node_modules folder would contain only those packages that are in my package.json file. For example, if I have installed only vinyl-source-stream package then my node_modules contains only one child folder. vinyl-source-stream folder would contain its dependencies in subfolders.

With npm 3, the root node_modules contains all packages that are not in version conflict.

Let's take my website as an example.

My package.json lists 30 packages as direct dependencies, but node_modules contains 535 folders instead of 30! The package is moved to the "root" level if there is no conflict between versions.

The flat hierarchy gives several benefits, such as a same package will not be stored multiple times also this will decrease the risk of having super-long path names to a minimum.

There might be problems if you or package vendor has used `require´ in a way that relies on a hierarchy, but those should be rare.

Try npm 3 and share your experience!