Home

Awesome

is-electron-renderer

Check if code is running in Electron renderer process.

Why?

Electron code can run in either the main process or the renderer process. This is the same as asking if the code is running in a web page with access to the DOM or not. Read more here: https://github.com/atom/electron/blob/master/docs/tutorial/quick-start.md

Use Cases:

Install

npm i --save is-electron-renderer

Usage

You'll notice that when using console.log in Electron that in the renderer process outputs some weird log level garbage to stderr before your actual console message. You can normalize this behavior:

console-hook.js:

// clean up Electron output
function hook () {
  var isRenderer = require('is-electron-renderer')
  var pre = '(' + (isRenderer ? 'RENDERER' : 'MAIN') + ') '
  console.log = function (msg) {
    process.stdout.write(pre + msg + '\n')
  }
}

module.exports = {
  hook: hook
}

index.js:

require('./console-hook').hook()
console.log('hello')

output (main):

(MAIN) hello

output (renderer):

(RENDERER) hello

API

var isRenderer = require('is-electron-renderer')
console.log(isRenderer)
// => (BOOLEAN)

License

MIT

Copyright 2015 JP Richardson