For Devs

Injecting Node Modules

Injecting Modules?

JavaScript has two main environments - these provide access to the file system, network requests, etc.

  • Web environment
  • Node.js

As we’re using Electron, our software defaults to the web environment. Browsers like this are generally more restrictive, but Node provides access to everything we need. Because of this, we load our dependencies inside the Node environment and pass them to the rest of our application using the method below. This allows the same Node dependency to be used throughout our code.

We don’t use Electron’s nodeIntegration for this because it violates the principle of least privilege, and has the capability of doing everything - this leads to issues with security.

Using Node Modules

If you are trying to use Node modules within the frontend then:

  1. Navigate to the ./ui/electron directory.
  2. Install the module using:
yarn add <module-name>
  1. Go to ./ui/electron/src/preload.js and import your module and attach it to window.
const nodeModule = require('<module-name>')

window.nodeModule = nodeModule
  1. Go to ./ui/reactui/src/nodeImports.js and import module from window.
const nodeModule = window.nodeModule

export {nodeModule}
  1. Import injected module anywhere throughout the frontend.
import { nodeModule } from './nodeImports'