Getting error Cannot destructure property 'protocol' of 'window.location' as it is undefined in nextjs application while building it
Asked Answered
H

1

9

I am getting the error below in my nextjs application while building it. My nextjs application is configured with typescipt for building reasons though i am using js only.

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.

This is my tsconfig.json file:

{
  "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"
  ]
}

I tried googling this bug but nothing relevant came back as a result.

I searched in my folder and in .next directory files i found this

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

I am not sure what i am doing wrong?

Haggadah answered 28/10, 2020 at 21:21 Comment(0)
H
2

I had this error when running with next dev.

The reason was that I had exportTrailingSlash: true in my next.config.js. So for example if I opened /about it would produce that error, but if I opened /about/ everything would work perfectly.

Haloid answered 6/5, 2021 at 13:3 Comment(1)
But the OP doesn't have exportTrailingSlash property set up...Economically

© 2022 - 2024 — McMap. All rights reserved.