title: Electron - IPC changes the object
body: <p>In my Electron project, I'm trying to make a module singleton by setting it as a global. Since I use <code>jquery</code> in this module, I import it in the renderer process and then send it to the main via ipc and set it as global there. Here is related part of my code:</p>
  
  <p>main.js:</p>
  
  <pre><code>ipcMain.on( "setGlobal", ( event, global_var) =&gt; {
    global[global_var[0]] = global_var[1];
    console.log(global_var);
    event.returnValue = 1;
  } );
  </code></pre>
  
  <p>renderer.js:</p>
  
  <pre><code>const favourites = require("./components/favourites");
  
  console.log(favourites);
  
  ipcRenderer.sendSync("setGlobal", ["favourites", favourites]);
  
  console.log(remote.getGlobal("favourites"));
  </code></pre>
  
  <p>The outputs of <code>console.log</code>s in the renderer process are in the image below:</p>
  
  <p><a href="https://i.stack.imgur.com/UDZ9R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UDZ9R.png" alt="renderer output"></a></p>
  
  <p>And the output of the main process is:</p>
  
  <pre><code>[ 'favourites', { favourites: [] } ]
  </code></pre>
  
  <p>As you see, the object (module) I sent from the ipcRenderer is changed in the ipcMain, it lost its <code>add</code> and <code>init</code> functions. Do you have any idea what is the reason of this behavior and how to fix it?</p>
  
  <p><strong>PS: To be sure, I tested it with a simple objects that contains functions instead of <code>require("favourites")</code>. They are also behaving in the same way. I did a workaround by using only the entities as global and passing them to all of the functions as arguments. However, it's not a good way in sense of code readability.</strong></p>