title: Electron :how to close app via js in 2021?
body: <p>I want to close the Electron App by js.</p>
  <pre><code>&quot;electron&quot;: &quot;^13.1.7&quot;
  </code></pre>
  <p>I read those questions carefully:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/31171597/atom-electron-close-the-window-with-javascript">Atom Electron - Close the window with javascript
  </a></li>
  <li><a href="https://stackoverflow.com/questions/43314039/how-to-close-electron-app-via-javascript">How to close electron app via javascript?</a></li>
  <li><a href="https://stackoverflow.com/questions/46653325/close-electron-app-on-click-event">Close Electron app on click event</a></li>
  </ul>
  <p>But none of those work for me.</p>
  <hr />
  <p>All the below tests are base on <a href="https://github.com/electron/electron-quick-start" rel="nofollow noreferrer">the offical electron-quick-start</a></p>
  <ol>
  <li><h4 id="according-to-the-answers-in-questions-above-i-got-these-code-f14w">According to the answers in questions above, I got these code:</h4>
  </li>
  </ol>
  <p><strong>index.html</strong></p>
  <pre class="lang-html prettyprint-override"><code>  &lt;body&gt;
      &lt;button id=&quot;close-app&quot;&gt;
          close
      &lt;/button&gt;
      &lt;script src=&quot;./renderer.js&quot;&gt;&lt;/script&gt;
    &lt;/body&gt;
  </code></pre>
  <p><strong>main.js</strong></p>
  <pre class="lang-js prettyprint-override"><code>const {BrowserWindow, remote} = require('electron')
  ...
  app.whenReady().then(() =&gt; {
    ...
    app.on('activate', function () {
      ...
      document.getElementById(&quot;close-app&quot;).addEventListener(&quot;click&quot;, function (e) {
        remote.getCurrentWindow().close()
      })
    })
  })
  </code></pre>
  <p>There is just no any effects or errors.</p>
  <p><a href="https://i.stack.imgur.com/GttUE.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GttUE.gif" alt="There is just no any effects or errors." /></a></p>
  <p>It seems like the code have never been run, I added a <code>console.log('run')</code> above the <code>addEventListener</code> and there is nothing been print in the console.</p>
  <hr />
  <ol start="2">
  <li><h4 id="according-to-the-document-i-got-these-code-eq0f">According to the <a href="https://www.electronjs.org/docs/api/app" rel="nofollow noreferrer">document</a>, I got these code:</h4>
  </li>
  </ol>
  <p>(change nothing of <a href="https://github.com/electron/electron-quick-start" rel="nofollow noreferrer">the offical electron-quick-start</a> but only <code>preload.js</code> and <code>index.html</code>)</p>
  <p><strong>index.html</strong></p>
  <p>as same as above</p>
  <p><strong>preload.js</strong></p>
  <pre><code>const { app } = require('electron');
  window.addEventListener('DOMContentLoaded', () =&gt; {
    document.getElementById(&quot;close-app&quot;).addEventListener(&quot;click&quot;, function (e) {
        app.quit();
    })
  })
  </code></pre>
  <p>got <code>Uncaught TypeError: Cannot read property 'quit' of undefined</code> only
  <a href="https://i.stack.imgur.com/0cUst.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0cUst.png" alt="Uncaught TypeError: Cannot read property 'quit' of undefined" /></a></p>
  <hr />
  <p>So, how can I close the Electron App by js?</p>
  <p>Thanks a lot for anyone help!</p>