Search code examples
node.jsnpmelectronnode-gypelectron-rebuild

Unable to build native packages for Node


I'm running an Electron app that has some native modules of one them being a package called better-sqlite3. I run Yarn and Yarn is able to install all packages without a problem but when it gets to the building steps, everything breaks.

I get a bunch of different node-gyp error outputs of which 2 I have been able to save so I can show here here is the important part of the error output for the first error:

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\IDE\VC\VCTargets\Microsoft.Cpp.Platform.targets(67,5): error MSB8020: The build tools for v140 (Platform Toolset = 'v140') cannot be found. To build using the v140 build tools, please install v140 build tools. Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the solution, and then selecting "Retarget solution". [C:\Users\bepop\Documents\DeveloperData\Clippy\app\node_modules\better-sqlite3\build\deps\action_before_build.vcxproj]

Full error

In terms of this error I've tried doing multiple things such as installing Microsoft visual build tools in many different ways such as manually going to their website and downloading the IDE, I've also tried installing the build tools through NPM with:

npm install --global --production windows-build-tools

It always successfully installs the build tools but still it errors out, I've also tried editing the npm config with:npm config set msvs_version 2017 still doesn't work. I have also tried going to the installation of the Visual Studio and manually checking: "VC++ 2017 version 15.7 v14.14 latest v141 tools". Also nothing.

I do remember at some point I was able to get everything to build normally with Windows 10 and I never had a problem but not any more. I've been trying to fix this issue by myself for months now and I've just been sticking on working on the project on my MacBook where it never has issues building the better-sqlite3 package.

One more thing I have tried is completely wiping my computer from everything and starting fresh because at some point I had changed so many things that I lost track of what I had installed and what I did not.

If I remove the better-sqlite3 package from the package.json file everything runs smoothly and I am able to run my project normally but I need the better-sqlite3 package. I could use another package but I really shouldn't have to do that, I'd like to get to the bottom of why this is not building.

Edit

I added Microsoft build tools v14.0.23107.0 to my pats variable and now it outputs error:

C:\Users\bepop\Documents\DeveloperData\Clippy\node_modules\integer\build\integer.vcxproj(21,3): error MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.


Solution

  • Fixed the issue for myself:

    1. Delete node modules folder

    1. Open PowerShell with admin privileges and run in order

      npm install --global --production --vs2015 --add-python-to-path windows-build-tools

      npm install --global --production --add-python-to-path windows-build-tools node-gyp

    1. Go to ~/.npmrc and make sure these are your settings:

      msvs_version=2015

      python=python2.7