获取错误无法解构“ window.location”的属性“ protocol”,因为在构建nextjs应用程序时未定义该属性

问题描述

在构建它时,我的nextjs应用程序中出现以下错误。尽管我仅使用js,但出于构建原因,我的nextjs应用程序配置了typecipt。

Build error occurred:

TypeError: Cannot destructure property 'protocol' of 'window.location' as it is undefined.
    at getLocationorigin (/var/www/Tradersnode/html/Tradersnode/client/mpanel/node_modules/next/dist/next-server/lib/utils.js:3:162)
    at Object.<anonymous> (/var/www/Tradersnode/html/Tradersnode/client/mpanel/node_modules/next/dist/next-server/lib/router/utils/parse-relative-url.js:1:206)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Module.require (internal/modules/cjs/loader.js:1042:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (/var/www/Tradersnode/html/Tradersnode/client/mpanel/node_modules/next/dist/next-server/lib/router/router.js:1:558)
    at Module._compile (internal/modules/cjs/loader.js:1156:30) {
  type: 'TypeError'
}
error Command Failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

这是我的tsconfig.json文件

{
  "compilerOptions": {
    "target": "es5","lib": [
      "dom","dom.iterable","esnext"
    ],"allowJs": true,"skipLibCheck": true,"strict": false,"forceConsistentCasingInFileNames": true,"noEmit": true,"esModuleInterop": true,"module": "esnext","moduleResolution": "node","resolveJsonModule": true,"isolatedModules": true,"jsx": "preserve"
  },"include": [
    "next-env.d.ts","**/*.ts","**/*.tsx"
  ],"exclude": [
    "node_modules"
  ]
}

我尝试谷歌搜索错误,但结果没有任何相关结果。

我在自己的文件夹和.next目录文件中进行搜索

 `var protocol = window.location.protocol; // Don't warn in exotic cases like chrome-extension`

我不确定我在做什么错吗?

解决方法

使用 next dev 运行时出现此错误。

原因是我的 exportTrailingSlash: true 中有 next.config.js。 因此,例如,如果我打开 /about,它会产生该错误,但如果我打开 /about/,一切都会正常运行。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...