Search code examples
javascriptnode.jsbrowserifyelectronchromium

What does it mean for Electron to combine Node.js and Chromium contexts?


In a blog post the author mentions that Electron combines Node and Chromium into a "single context" which implies that we don't have to use Browserify to transform code.

I understand that one implication of Electron is you can build cross-platform desktop apps using web technologies. I also understand the reason why we're able to write to the filesystem is because Electron has Node baked in. Also, the reason we're able to use HTML/CSS/JS/DevTools is because Chromium is baked in. However, I don't think this is what the author is talking about.

  • How does Electron to combine Node and Chromium into a "single context"?
  • Why don't have to use Browserify anymore?

Solution

  • Chromium is a Webkit based web browser with the V8 javascript engine. It supports all the usual browser and DOM APIs and thus is good for making web pages and not good at interacting with the underlying system.

    Node.js was built by striping out the V8 engine, making a headless command line application, and adding extensive APIs to access the file system, require() other files, run other shell programs, etc. (things you'd expect of a true scripting language.

    Electron in a simplified way is an attempt to replace the V8 engine used in Chromium with the new more general purpose oriented one of Node.js. It exposes a few extra APIs to node.js to allow for opening chromium windows, but also every chromium window using a <script> tag will interpret it with the node.js engine.

    Why Electron? The reason that Chromium can't do this by itself is because it was originally designed to be a web browser and in web browsers file system APIs would be unheard of as typically files are hosted on a remote server and accessing files on a user's computer would be a security risk (because why should any single webpage have access to all your files?).

    require statements now work out of the box because node.js has filesystem support will allows them to be synchronously read from the disk without the need for bundling them into the same javascript file or requesting them from a server.