'next build error occrred from performace-relayer

next build occurred error by

ReferenceError: location is not defined
    at Object.<anonymous> (/PATH/node_modules/next/dist/client/performance-relayer.js:1:135)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/PATH/node_modules/next/dist/client/index.js:1:1406)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) {
  type: 'ReferenceError'

I don't have any idea..

plz help



Solution 1:[1]

Generally, it happens because of incorrect import of NextJS components such as Link, router

For instance:

import {router} from "next/client";   // will cause location not found error

will cause this type of error. The correct way is as follows:

import router from "next/router";    // correct way

Same is with "Link":

import Link from "next/link";    // correct way

Solution 2:[2]

We had same problem. In our case, these were incorrect imports of router:

import { NextRouter } from "next/dist/client/router";
import { useRouter } from 'next/dist/client/router';
import { router } from "next/client";

I changed two first to "next/router" and replace the last one by using useRouter()

Solution 3:[3]

ReferenceError: location is not defined is occurred due to the wrong import of next js package.

Suppose if you are using the router for routing pages using command push router.push('/path'),so we need to import router from import {useRouter} from "next/router" then use router like this :

const router=useRouter();

router.push('/path');

Note: No need to import from "next/client" like this:

import {router} from "next/client";// Next build error occurred from performance-relayer

I think it will solve your problem. Doc ref:- https://nextjs.org/docs/api-reference/next/router

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ismoil Shokirov
Solution 3