Sholly Creative, Inc.

The Simpler Way to Rebuild Native Modules in Electron

This issue has plagued me more than I care to admit. Since Electron is developing at a furious rate, you may have a new Electron version to compile to every few days it feels. Each time they update something in the core, you’ll have to recompile any native modules that your project might be using.

In my case, I was using paulcbetts/node-system-idle-time. Each time I upped the Electron version, I would get 5 different compile, node, or app errors.

Just Rebuild they say…

When in doubt, run electron-rebuild they say. Use node-pre-gyp. Set a few environmental variables. None of these had anything close to a good success rate.

Start from scratch

Maybe its my NPM scripts, maybe its since I’ve done much of the work on a Windows machine. Whatever the case, the most successful technique is to set your npm environmental variables like so. Type this in your favorite CLI at the project root.

Take special care to update the npm_config_target of your current Electron version installed.

Once those are set, delete your node_modules folders. In a few projects, there’s 2 – one in the root and one in the app. Other projects just one.

Then run your standard npm install commands, and this usually results in a working app once again.

Your mileage may vary. I suspect my stuggles come from a user permission issue, or maybe I need a clean task added somewhere.

Don’t wait for Windows to carefully delete

Node_modules folders can get HUGE nowadays. Asking Windows to delete it the typical way is a test of your patience. Instead nuke it with your command like.

    • Open CMD from your Windows Start Menu, or with your favorite technique.
    • Navigate to your project folder’s root
    • Type rmdir /s/q node_modules

This will silently AND WITHOUT CONFIRMATION, delete your node_modules folder. Don’t say I didn’t warn you.

That’s my life-hack to get Native Modules working in Electron again. What are yours?