Configuring NodeJS in Windows for Success

Here’s how configuring NodeJS a bit different from its installed settings can create a little less of a headache in Windows.

NodeJS Logo

If you’re not familiar, NodeJS is an awesome environment for developing apps using JavaScript run with Chromium’s V8 engine.

I’ve been using Node on my Ubuntu a little bit of time. Recently I’ve been working on projects at my work while in Windows. It didn’t take long after installing some packages that I ran into most developers common annoyance with developing in a Windows environment, path length limitations. It’s 2015 and users are still dealing with a 260 character limit on the path. It isn’t all too common when building a highly structured project layout that you can get a very highly nested folder structure.

With Node this is more of a problem since each module you ‘npm install’ has its own modules which are installed along with it. Each of those modules may have a dependency on a module which is in turn installed, etc.  Since this can go on for an indefinite level of recursion one can quickly run up against Windows’ path limitations.  The default path for the package manager and its cache is in <%CURRENT_USER_FOLDER%>\AppData\Roaming.  This makes the hitting the path length limit almost guaranteed!

Configuring NodeJS to Solve the Issue

Here’s an easy way to gain some path length back. It doesn’t solve the problem, but it does help out a bunch.

  1. Open up a NodeJS prompt.
  2. Run the two commands at the end of this list.
  3. Close the prompt window
  4. Open up your list of environment variables.
    (Start Menu -> Right-click Computer -> Select Properties -> On left side of the window which opens select “Advanced system settings” -> At the bottom of the dialog click “Environment Variables” button)
  5. Change your User (the top section) PATH variable such that the path to npm is what you set in the command you ran earlier.
    ie C:\Users\me\AppData\Roaming\npm becomes C:\npm
  6. Close out the dialogs by click “OK”.
  7. Open a new NodeJS prompt and make sure that npm now works.
  8. You can check your settings for npm at any time by typing in “npm config ls -l”. This should show the new paths.

First command changes the location of the prefix, or npm’s operating directory.

npm config set prefix C:\npm

Second command changes the cache location.

npm config set cache C:\npm-cache

 

About Mike

I'm a software engineer. Look into the about page for more information about me.
Tagged , , . Bookmark the permalink.

2 Responses to Configuring NodeJS in Windows for Success

  1. Benson Trent says:

    The backslash character can be escaped, resulting in a prefix and cache that looks like C:npm.

    To correct this, escape the backslash character with another backslash.

    npm config set prefix C:\\npm
    npm config set cache C:\\npm-cache

    • Mike says:

      Interesting. I’ve not had to escape the backslash when using the command. Running ‘npm config get prefix’ or ‘npm config get cache’ show a correctly formatted path.

      Were you having an issue with this?

Leave a Reply