title: Why can't variables declared outside a function be used within it?
body: <p>This code should work but the error message is "Cannot deconstruct</p>
  
  <pre><code>const fs = require('fs');
  let {  remote: { app  }  } = require('electron');  
  let userDataPath = app.getPath('userData');
  let filePath = path.join(userDataPath, 'preferences.json')
  
  const readPreferences = () =&gt; {
      return JSON.parse( fs.readFileSync(filePath) )
  }
  const writePreferences = (inputs) =&gt; { 
      inputs &amp;&amp; fs.writeFileSync(filePath, JSON.stringify(inputs));
  }
  
  module.exports = {readPreferences, writePreferences };
  </code></pre>
  
  <p>But if I add the all the <code>let</code> variables inside each function, the code works.</p>
  
  <p>Is that normal? why does that happen.</p>
  
  <p>And should be repeating those there? </p>
  
  <p>I tried this in a separate demo and it all works fine:</p>
  
  <pre><code>// - two.js
  let two = "this is something"
  exports.one = () =&gt; {  
      return two
  }
  
  // - index.js
  const { one } = require('./two')
  let two = "anything"
  console.log(one()) // -&gt; this is something
  </code></pre>
  
  <p>Edit: This works though</p>
  
  <pre><code>const fs = require('fs');    
  const readPreferences = () =&gt; {
  let {  remote: { app  }  } = require('electron');  
  let userDataPath = app.getPath('userData');
  let filePath = path.join(userDataPath, 'preferences.json')
      return JSON.parse( fs.readFileSync(filePath) )
  }
  const writePreferences = (inputs) =&gt; { 
  let {  remote: { app  }  } = require('electron');  
  let userDataPath = app.getPath('userData');
  let filePath = path.join(userDataPath, 'preferences.json')
      inputs &amp;&amp; fs.writeFileSync(filePath, JSON.stringify(inputs));
  }
  
  module.exports = {readPreferences, writePreferences };
  </code></pre>