1

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 "doesUserContainRoles" to the exports, my Chrome Console gives me the error:

Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

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:

const utils = require("../../../utils");

And there are no imports used with this. (I know that module.exports and import don't work together)

What I am using:

  • Vue.js @vue/cli 4.5.8

  • Node.js v15.2.0

Files:

utils.js

const winston = require("winston");
const { datadog } = require("./credentials.js");
const { createLogger, format, transports } = require('winston');
const base_url = "http://localhost:3001/api/v1"

// 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: "User Accessed a Page",
  restrictedPage: "Attempt at accessing restricted page"
}

function alertGeneral() {
  alert("Oops! An error has occurred. Please try again later. If this problem continues, please contact Vinniehat.");
}
function doesUserContainRoles(userRoles, containsRoles) {
  return !!userRoles.some(role => containsRoles.includes(role));
}

module.exports = {
  logger,
  commonMessages,
  base_url,
  alertGeneral,
  doesUserContainRoles
}

Here is the error stack:

Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
    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)

I have tried to use export default 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:

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.<anonymous> (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)
Vincent Lauro
  • 31
  • 1
  • 10
  • Let start simple. Change to - const doesUserContainRoles = (userRoles, containsRoles) => !!userRoles.some(role => containsRoles.includes(role)) beside that add the full stack trace – Michael Hobbs Dec 05 '20 at 20:29
  • @MichaelHobbs Ok! I've changed that on over and added the stack trace to the original post. – Vincent Lauro Dec 05 '20 at 20:38
  • are you running webpack on your server code? I also see eval on main.js I have a feeling a lot it going on here. You should push this to github and provide a link. You are doing something very strange to be getting this error. – Michael Hobbs Dec 05 '20 at 20:44
  • The utils file is being required in an express file if that is what you mean. Also the repository is unfortunately private. I would be happy to provide any files that you need though. Sorry about that! – Vincent Lauro Dec 05 '20 at 20:47
  • Since it’s an error thrown by webpack: have you tried using ES6 module `export` instead? – Terry Dec 05 '20 at 20:48
  • I have tried using ```export default {}``` before, in which my website loads, I don't get an error on Chrome, but my backend(run with Express, then errors out). I am updating the original post to include the error stack if I were to change to export default. Edit: The same error does occur without ```default``` – Vincent Lauro Dec 05 '20 at 20:54
  • Are you trying to include this utils file both in the frontend project and in the backend project? – Bergi Dec 05 '20 at 21:22
  • Correct. The utils is being used on both – Vincent Lauro Dec 05 '20 at 21:40

1 Answers1

0

Somewhere in main.js > router.js > utils.js you are mixing CommonJSModules and ESModules. Something along the lines of CommonJSModule --requires--> ESModule --requires--> AnotherCommonJSModule

Edit: This seems to be relevant as well https://stackoverflow.com/questions/42449999/webpack-import-module-exports-in-the-same-module-caused-error

module.exports = {
  presets: [
    '@quasar/babel-preset-app'
  ],

  "sourceType": "unambiguous"
}
Michael Hobbs
  • 1,663
  • 1
  • 15
  • 26