This course is out of date and does not reflect our standards or industry best practices.

Check out a free preview of the full Electron Fundamentals, v2 course:
The "electron-rebuild" Lesson is part of the full, Electron Fundamentals, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Steve introduces a solution to the problem that the version of Nodejs is different than the version that is installed on a local machine.

Get Unlimited Access Now

Transcript from the "electron-rebuild" Lesson

>> Steve Kinney: I still have the problem that none of this persists, and maybe that's okay. Maybe this is a temporary application. It's not supposed to hold on to that stuff. But it also seems like a really great excuse to talk about using native modules. So [LAUGH], here we are.

[00:00:16] So let's talk a little bit about the state of native modules in Electron applications in general. And then we'll talk about a little bit in specific with Electron-Forge. Because some of that is taken care for us already. But you might have an Electron application that you're working with that doesn't use Electron-Forge and stuff like that.

[00:00:33] So it's good to talk about how it works and how to understand what's going on. All right, cool. Talk a little bit about, we saw electron compile, and electron compile was a library that let you transpile Babble and TypeScript and SASS and stuff to JavaScript and CSS respectively.

[00:00:51] Electron-rebuild solves a slightly different problem. The problem is the version of Node on your system is different than the version of Node Electron that you're using. With Electron, one of the great things is that we are taking our own version of Chrome and our own version of Node and sending it to our users.

[00:01:11] They don't even, like somebody who downloads Slack does not need to have Node installed on their computer, right? There is a version of Node bundled with the Slack application, right? And that is really great. The problem is, when you do an MPM install, if it is a compiled module, right, most modules are compiled against a given version of V8 for a given architecture, right?

[00:01:32] But more importantly the given version of V8, which is if you've ever like you have maybe, Node Sass is usually the one bites me. You do an NPM install, then you NVM to a different version of Node. You start up the application, it blows up cuz Node Sass is very angry, cuz it's compiled for Node V8, not Node V9, so on and so forth.

[00:01:53] Yeah, if you have a complied module compiled against the wrong version of V8, it doesn't work. So this is a problem, because if you do an NPM install, it's going to be the same as the one on your system. Which would work if the version of Electron that you happen to be using has the exact same version of Node as the version of Node running on your system.

[00:02:13] Chances of that happening? Not great. So, that's our problem. The solution is we use electron-rebuild which is over on line 19 in this package JSON as a post install step, right? So after we do an NPM install for whatever it is. Whether it's Node Sass, whether it's the SQLite driver that we're gonna be using in a second.

[00:02:37] Whatever it is, go ahead and recompile it for the version of Electron cuz that's where we're gonna be using it. So what it does is, yeah, you go ahead and install everything and then it actually builds it against Electron. So that solves our problem of having the version that we installed different from the version the Electron needs.

[00:02:55] It just gives us the version that Electron needs. This is so useful and such a good idea that it is built into Electron Forge by default. Right, and so if you watch those little check boxes, as we started, it was like checking native dependencies or something along those lines, it was doing that when we started up the application.

[00:03:16] This is generally speaking a PSA that I'm doing right now to basically just make the point of, if you're not using Electron Forge, and you're like I watched this workshop, I inherited this app, or I have an app, I'm just choosing not to use it, whatever. And now I went to go use SQLite, cuz Steve told me I could, and everything blows up.

[00:03:36] This is the very useful PSA that I am cleared of all responsibility for that, so that's better.