Windows 10 node-gyp cannot build: MSBUILD failed with exit code 1

11,467

Solution 1

Edit: The question in title is a bit different from OP's actual issue (which this answer addresses).
For node-gyp, or MSBUILD errors in general please see other questions.

To solve the "The specified path, file name, or both are too long." issue, update npm to version 3+. This has been bothering all windows users for a while now, and the answer is to flatten your dependencies. [email protected] automatically flattens all dependencies to the root even during install.

Solution 2

Long file paths is a big issue with Visual Studio that Microsoft has stated that they have no plans to fix in the near future!
Try moving your solution folder to the root of the drive (c:\project)
Also NPM version 3+ changes the module folder structure so they are not nested so deep. (it says it's in beta but seems to have solved some problems for me)

Regarding your post on Github, (I've been running into an endless string of problems with NPM and node-gyp as well, apparently everything is made to work well on linux!)
You probably need to install VS2013 (you can use the express version, make sure it includes c++)
From the npm errors on github:
v120 is VS2013
v140 is VS2015

To make node-gyp use the correct version for the package you are installing you may need to set an environment variable in Windows.
set GYP_MSVS_VERSION=2013
(if you set it in your windows system properties make sure to restart your command prompt)

Solution 3

By default in MS Windows 10, the NPM global install path will use your user profile directory;

%USERPROFILE%\AppData\Roaming\npm
%USERPROFILE%\AppData\Roaming\npm-cache

Which translates to something like;

C:\Users\YOUR-USER-PROFILE-NAME\AppData\Roaming\npm
C:\Users\YOUR-USER-PROFILE-NAME\AppData\Roaming\npm-cache

The first path above contains 51 characters and the second contains 57 characters. Depending on the length or your user profile name, your path may be a little shorter or longer.

So the aim is to change the directories used when you install anything with NPM globally by using the -g flag to use directories with fewer characters.

Change the "global" NPM folders to %ALLUSERSPROFILE%\(npm|npm-cache)

  1. Your user account should be a member of the administrators group or you may need to enter your administrator password when prompted.
  2. Copy the existing 'npm' folder from %USERPROFILE%\AppData\Roaming\ and paste into %ALLUSERSPROFILE%
  3. Open a Windows command prompt and run the following commands;

    npm config --global set prefix "C:\ProgramData\npm"
    npm config --global set cache "C:\ProgramData\npm-cache"
    

    This will add/update the contents of: %USERPROFILE%\AppData\Roaming\npm\etc\npmc with;

    prefix=C:\ProgramData\npm
    cache=C:\ProgramData\npm-cache
    

    The first path above now only contains 18 characters and the second contains just 24 characters.

Add/change the PATH variable for NPM

  1. Navigate to 'Environment Variables' by following these steps;

    Start > Settings > System > About > System info > Advanced system settings

    Under the 'Advanced' tab click the 'Environment Variables...' button.

  2. Select the 'PATH' variable and click the 'Edit...' button.

  3. If there is already an entry for NPM like C:\Users\YOUR-USER-PROFILE-NAME\AppData\Roaming\npm, select it and click the 'Edit' button. Otherwise, click the 'New' button.

  4. Paste C:\ProgramData\npm and click the 'OK' button to save.

  5. Finally, restart your computer.

Share:
11,467
nerijusgood
Author by

nerijusgood

Hello there :) I am motivated front-end web & UI designer, developer. Passionate about creating beautiful and visually stunning work, strict towards coding standards and experienced in developing cross platform web experience. I am confident, well-rounded, and completely awesome web designer, who knows how to code. PM and say Hi :)

Updated on June 23, 2022

Comments

  • nerijusgood
    nerijusgood almost 2 years

    Windows 10, Microsoft Visual Studio 2013, Node v0.12.7, node-gyp v2.0.2

    I always get the error build error with node-gyp:

    Error: `C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe` failed with exit code: 1
    

    Also in red color description:

    Could not write lines to file "Release\obj\validation\validation.tlog\validation.lastbuildstate". The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. 
    

    Bigger excerpt of error: https://gist.github.com/nerijusgood/63e54d9c376999a044bb

    As I understand everything is running fine, however msbuild cannot write long path names. Is there a patch for this or windows workaround?