├── .gitignore ├── changelog.md ├── index.js ├── package.json ├── readme.md └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | 2.0.1 / 2016-09-25 2 | ------------------ 3 | - fix checking if `process` exists. 4 | 5 | 2.0.0 / 2015-07-14 6 | ------------------ 7 | - examine `process` instead of `global`. See: https://github.com/jprichardson/is-electron-renderer/pull/1 8 | 9 | 1.0.0 / 2015-07-04 10 | ------------------ 11 | - initial release 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function isRenderer () { 2 | // running in a web browser 3 | if (typeof process === 'undefined') return true 4 | 5 | // node-integration is disabled 6 | if (!process) return true 7 | 8 | // We're in node.js somehow 9 | if (!process.type) return false 10 | 11 | return process.type === 'renderer' 12 | } 13 | 14 | module.exports = isRenderer() 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-electron-renderer", 3 | "version": "2.0.1", 4 | "description": "Check if code is running in Electron renderer process.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "standard && mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/jprichardson/is-electron-renderer.git" 12 | }, 13 | "keywords": [ 14 | "electron", 15 | "electron-component", 16 | "atom", 17 | "renderer", 18 | "process" 19 | ], 20 | "author": "JP Richardson", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/jprichardson/is-electron-renderer/issues" 24 | }, 25 | "homepage": "https://github.com/jprichardson/is-electron-renderer#readme", 26 | "devDependencies": { 27 | "mocha": "2.x", 28 | "standard": "4.x" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | is-electron-renderer 2 | ==================== 3 | 4 | Check if code is running in Electron `renderer` process. 5 | 6 | Why? 7 | ---- 8 | 9 | Electron code can run in either the `main` process or 10 | the `renderer` process. This is the same as asking if 11 | the code is running in a web page with access to the 12 | DOM or not. Read more here: https://github.com/atom/electron/blob/master/docs/tutorial/quick-start.md 13 | 14 | ### Use Cases: 15 | 16 | - Creating a single module that acts differently whether it's running in `main` or `renderer`. 17 | - Logging utility. One process (`main`) would be responsible for writing to log files, while 18 | renderers would send log data to the `main`. Would allow your code to have one `log` method. 19 | - Testing. Your test code may behave differently if the DOM is available. 20 | - Normalize `console.log` behavior. `console.log` behavior is weird in `renderer`, this can easily be fixed. 21 | 22 | Install 23 | ------- 24 | 25 | npm i --save is-electron-renderer 26 | 27 | 28 | Usage 29 | ----- 30 | 31 | You'll notice that when using `console.log` in Electron that in the `renderer` process 32 | outputs some weird log level garbage to `stderr` before your actual console message. 33 | You can normalize this behavior: 34 | 35 | **console-hook.js**: 36 | 37 | ```js 38 | // clean up Electron output 39 | function hook () { 40 | var isRenderer = require('is-electron-renderer') 41 | var pre = '(' + (isRenderer ? 'RENDERER' : 'MAIN') + ') ' 42 | console.log = function (msg) { 43 | process.stdout.write(pre + msg + '\n') 44 | } 45 | } 46 | 47 | module.exports = { 48 | hook: hook 49 | } 50 | ``` 51 | 52 | **index.js**: 53 | 54 | ```js 55 | require('./console-hook').hook() 56 | console.log('hello') 57 | ``` 58 | 59 | output (main): 60 | 61 | (MAIN) hello 62 | 63 | output (renderer): 64 | 65 | (RENDERER) hello 66 | 67 | 68 | API 69 | --- 70 | 71 | ```js 72 | var isRenderer = require('is-electron-renderer') 73 | console.log(isRenderer) 74 | // => (BOOLEAN) 75 | ``` 76 | 77 | License 78 | ------- 79 | 80 | MIT 81 | 82 | Copyright 2015 [JP Richardson](https://github.com/jprichardson) 83 | 84 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert') 2 | 3 | /* global describe it */ 4 | 5 | describe('isRenderer', function () { 6 | it('should return false in Node.js', function () { 7 | var isRenderer = require('./') 8 | assert(!isRenderer) 9 | }) 10 | }) 11 | --------------------------------------------------------------------------------