title: Electron: cannot read property 'getCurrentWebContents' of undefined
body: <p>I'm trying to read the files from a directory using the fs module, and display that in a div with the id displayfiles in an Electron app. I keep getting the error :</p>
  
  <pre><code>Cannot read property 'getCurrentWebContents' of undefined
  </code></pre>
  
  <p>I've tried a coupld of different things to get access to the webcontents of the main window in my menu.js but I keep getting undefined errors.
   My code in menu.js looks like this:
  Cannot read property 'getCurrentWebContents' of undefined
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const electron = require('electron');
  const dialog = electron.dialog;
  const fs = require('fs');
  const remote = require ("electron").remote;
  const template=[
      {
          label: 'File',
          submenu: [
            {
             label:'Open USB',
             click () { dialog.showOpenDialog( {
              properties: ['openDirectory']
            },directorySelectorCallback)
  
             }
            },
            {
              label:'Exit',
            click() {
  
            } 
            },
          ] 
      },
      {
          label: 'Edit',
          submenu: [
            {role: 'undo'},
            {role: 'redo'},
            {type: 'separator'},
            {role: 'cut'},
            {role: 'copy'},
            {role: 'paste'},
            {role: 'pasteandmatchstyle'},
            {role: 'delete'},
            {role: 'selectall'}
          ]
        },
        {
          label: 'View',
          submenu: [
            {role: 'reload'},
            {role: 'forcereload'},
            {role: 'toggledevtools'},
            {type: 'separator'},
            {role: 'resetzoom'},
            {role: 'zoomin'},
            {role: 'zoomout'},
            {type: 'separator'},
            {role: 'togglefullscreen'}
          ]
        },
        {
          role: 'window',
          submenu: [
            {role: 'minimize'},
            {role: 'close'}
          ]
        },
  ]
  function directorySelectorCallback(filenames) {
      console.log("test:");
      if (filenames &amp;&amp; filenames.length &gt; 0) {
          console.log(filenames[0]);
          fs.readdir(filenames[0], (err, files) =&gt; {
              'use strict';
              if (err) throw  err;
              //the files parameter is an array of the files and folders in the path we passed. So we loop through the array, printing each file and folder
              for (let file of files) {
                  //the += after innerHTML means we are appending to the existing content
                  remote.getCurrentWebContents().executeJavaScript(`
          document.getElementById("display-files").innerHTML += '&lt;li&gt; ${file} &lt;/li&gt;'
        `)
              }
          });
      }
  }
  module.exports.template=template;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here is my index.html file:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Rexnord&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;div &gt;&lt;ol id="display-files"&gt;&lt;/ol&gt;&lt;/div&gt;
      &lt;script&gt;
        // You can also require other files to run in this process
        require('./renderer.js')
        require('./menu/menu.js')
      &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here is my main.js file:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// Modules to control application life and create native browser window
  const electron = require('electron')
  const {app, BrowserWindow,Menu} = require('electron')
  const menuTemplate=require('./menu/menu.js')
  // Keep a global reference of the window object, if you don't, the window will
  // be closed automatically when the JavaScript object is garbage collected.
  var mainWindow
  
  function createWindow () {
    // Create the browser window.
    mainWindow = new BrowserWindow({width: 800, height: 600,icon: __dirname + '/Rexnord.ico'})
  
    // and load the index.html of the app.
    mainWindow.loadFile('index.html')
  
    // Open the DevTools.
     mainWindow.webContents.openDevTools()
    // Emitted when the window is closed.
    mainWindow.on('closed', function () {
      // Dereference the window object, usually you would store windows
      // in an array if your app supports multi windows, this is the time
      // when you should delete the corresponding element.
      mainWindow = null
    })
      let menu = Menu.buildFromTemplate(menuTemplate.template)
      Menu.setApplicationMenu(menu);
  }
  
  // This method will be called when Electron has finished
  // initialization and is ready to create browser windows.
  // Some APIs can only be used after this event occurs.
  app.on('ready', createWindow)
  
  // Quit when all windows are closed.
  app.on('window-all-closed', function () {
    // On macOS it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    if (process.platform !== 'darwin') {
      app.quit()
    }
  })
  
  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (mainWindow === null) {
      createWindow()
    }
  })
  
  // In this file you can include the rest of your app's specific main process
  // code. You can also put them in separate files and require them here.
  module.exports.mainWindow=mainWindow;</code></pre>
  </div>
  </div>
  </p>
  
  <p>How would I get access to the web contents of the main window in my menu.js file so I can manipulate the html?</p>