簡體   English   中英

如何修復“無法分配給 object 的只讀屬性“導出”'#<object> ' 將第二個 function 添加到 module.exports 時?<div id="text_translate"><p> 不幸的是,我已經被困了大約 5 天了。 我用谷歌搜索過,沒有什么對我有用。 我有一個 utils.js 文件,里面有很多東西可以幫助我。 當我將 function "doesUserContainRoles" 添加到導出時,我的 Chrome 控制台給了我錯誤:</p><blockquote><p> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;'</p></blockquote><p> 當我刪除 function 時,utils 文件中的所有內容都可以完美運行,根本沒有 0 個問題。 只要我添加 function,我就會收到此錯誤。 我通過以下方式要求 utils 文件:</p><pre class="lang-js prettyprint-override"> const utils = require("../../../utils");</pre><p> 並且沒有與此一起使用的import 。 (我知道module.exports和import不能一起工作)</p><p> 我正在使用什么:</p><ul><li><p> Vue.js @vue/cli 4.5.8</p></li><li><p> Node.js v15.2.0</p></li></ul><h3> 文件:</h3><p> utils.js</p><pre class="lang-js prettyprint-override"> 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 =&gt; containsRoles,includes(role)), } module,exports = { logger, commonMessages, base_url, alertGeneral, doesUserContainRoles }</pre><p> 這是錯誤堆棧:</p><pre> 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)</pre><p> 我也嘗試使用export default 。 這樣做時,我的前端工作。 網站加載沒有錯誤。 不過,我的后端使用 Express.js 運行,然后引發錯誤:</p><pre> 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)</pre></div></object>

[英]How do I fix 'Cannot assign to read only property 'exports' of object '#<Object>' when adding a 2nd function to module.exports?

不幸的是,我已經被困了大約 5 天了。 我用谷歌搜索過,沒有什么對我有用。 我有一個 utils.js 文件,里面有很多東西可以幫助我。 當我將 function "doesUserContainRoles" 添加到導出時,我的 Chrome 控制台給了我錯誤:

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

當我刪除 function 時,utils 文件中的所有內容都可以完美運行,根本沒有 0 個問題。 只要我添加 function,我就會收到此錯誤。 我通過以下方式要求 utils 文件:

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

並且沒有與此一起使用的import (我知道module.exportsimport不能一起工作)

我正在使用什么:

  • Vue.js @vue/cli 4.5.8

  • Node.js v15.2.0

文件:

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
}

這是錯誤堆棧:

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)

我也嘗試使用export default 這樣做時,我的前端工作。 網站加載沒有錯誤。 不過,我的后端使用 Express.js 運行,然后引發錯誤:

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)

main.js > router.js > utils.js的某個地方,您正在混合 CommonJSModules 和 ESModules。 CommonJSModule --requires--> ESModule --requires--> AnotherCommonJSModule的東西

編輯:這似乎也相關https://stackoverflow.com/questions/42449999/webpack-import-module-exports-in-the-same-module-caused-error

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

  "sourceType": "unambiguous"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM