出现错误无法解构“window.location”的属性“协议”,因为在构建它时,它在 nextjs 应用程序中未定义

Abd*_*mer 8 javascript build window.location typescript next.js

我在构建 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.
Run Code Online (Sandbox Code Playgroud)

这是我的 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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

我在我的文件夹和 .next 目录文件中搜索,我发现了这个

 `var protocol = window.location.protocol; // Don't warn in exotic cases like chrome-extension`
Run Code Online (Sandbox Code Playgroud)

我不确定我做错了什么?

小智 2

我在运行时遇到了这个错误next dev

原因是我exportTrailingSlash: truenext.config.js. 因此,例如,如果我打开/about它,就会产生该错误,但如果我打开,/about/一切都会正常工作。

  • 但OP没有设置`exportTrailingSlash`属性...... (2认同)