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 import
s 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)