title: How do I fix 'Cannot assign to read only property 'exports' of object '#<Object>' when adding a 2nd function to module.exports?
body: <p>I've unfortunately been stuck for about 5 days now on this. I've Googled and nothing has worked for me.
  I have a utils.js file with a bunch of stuff to help me out. When I add the function &quot;doesUserContainRoles&quot; to the exports, my Chrome Console gives me the error:</p>
  <blockquote>
  <p><code>Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;'</code></p>
  </blockquote>
  <p>When I remove the function, everything in the utils file works perfectly with 0 issues at all. It's just as soon as I add the function, I get this error. I am requiring the utils file by the following:</p>
  <pre class="lang-js prettyprint-override"><code>const utils = require(&quot;../../../utils&quot;);
  </code></pre>
  <p>And there are no <code>import</code>s used with this. (I know that <code>module.exports</code> and <code>import</code> don't work together)</p>
  <p>What I am using:</p>
  <ul>
  <li><p>Vue.js @vue/cli 4.5.8</p>
  </li>
  <li><p>Node.js v15.2.0</p>
  </li>
  </ul>
  <h3>Files:</h3>
  <p><code>utils.js</code></p>
  <pre class="lang-js prettyprint-override"><code>const winston = require(&quot;winston&quot;);
  const { datadog } = require(&quot;./credentials.js&quot;);
  const { createLogger, format, transports } = require('winston');
  const base_url = &quot;http://localhost:3001/api/v1&quot;
  
  // Winston ~
  const httpTransportOptions = {
    host: datadog.logger.host,
    path: datadog.logger.path,
    ssl: datadog.logger.ssl
  };
  const customLevels = {
    levels: {
      emergency: 0,
      alert: 1,
      critical: 2,
      error: 3,
      warn: 4,
      notice: 5,
      info: 6,
      debug: 7,
      success: 8
    },
  }
  const logger = createLogger({
    level: 'info',
    levels: customLevels.levels,
    exitOnError: false,
    format: format.json(),
    transports: [
      new transports.Http(httpTransportOptions),
    ],
  });
  
  const commonMessages = {
    accessPage: &quot;User Accessed a Page&quot;,
    restrictedPage: &quot;Attempt at accessing restricted page&quot;
  }
  
  function alertGeneral() {
    alert(&quot;Oops! An error has occurred. Please try again later. If this problem continues, please contact Vinniehat.&quot;);
  }
  function doesUserContainRoles(userRoles, containsRoles) {
    return !!userRoles.some(role =&gt; containsRoles.includes(role));
  }
  
  module.exports = {
    logger,
    commonMessages,
    base_url,
    alertGeneral,
    doesUserContainRoles
  }
  
  </code></pre>
  <p>Here is the error stack:</p>
  <pre><code>Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;'
      at Module.eval (utils.js?1b23:45)
      at eval (utils.js:68)
      at Module../utils.js (app.js:1944)
      at __webpack_require__ (app.js:854)
      at fn (app.js:151)
      at eval (router.js?9883:19)
      at Module../src/router/router.js (app.js:1812)
      at __webpack_require__ (app.js:854)
      at fn (app.js:151)
      at eval (main.js:34)
  </code></pre>
  <p>I have tried to use <code>export default</code> also. When doing this, my frontend works. The website loads with no errors. My backend though, which is ran using Express.js, then throws an error:</p>
  <pre><code>Unhandled rejection E:\Development\Websites\iceberg-gaming-website\utils.js:45
  export default {
  ^^^^^^
  
  SyntaxError: Unexpected token 'export'
      at wrapSafe (node:internal/modules/cjs/loader:1018:16)
      at Module._compile (node:internal/modules/cjs/loader:1066:27)
      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
      at Module.load (node:internal/modules/cjs/loader:967:32)
      at Function.Module._load (node:internal/modules/cjs/loader:807:14)
      at Module.require (node:internal/modules/cjs/loader:991:19)
      at require (node:internal/modules/cjs/helpers:92:18)
      at Object.&lt;anonymous&gt; (E:\Development\Websites\iceberg-gaming-website\express\modules\user.js:9:15)
      at Module._compile (node:internal/modules/cjs/loader:1102:14)
      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
      at Module.load (node:internal/modules/cjs/loader:967:32)
      at Function.Module._load (node:internal/modules/cjs/loader:807:14)
      at Module.require (node:internal/modules/cjs/loader:991:19)
      at require (node:internal/modules/cjs/helpers:92:18)
      at E:\Development\Websites\iceberg-gaming-website\express\express.js:27:27
      at tryCatcher (E:\Development\Websites\iceberg-gaming-website\node_modules\bluebird\js\release\util.js:16:23)
  </code></pre>