Next.js Discord

Discord Forum

TypeError: Cannot read properties of undefined (reading 'startsWith')

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
Hey guys,
I don't know where this error is occurring from, i don't have any startsWith in my code
When I click on a link for changing the URL I get an error :
TypeError: Cannot read properties of undefined (reading 'startsWith')
add-path-prefix.js:13 Uncaught TypeError: Cannot read properties of undefined (reading 'startsWith')


import authConfig from "@/auth.config"
import NextAuth from "next-auth"
 
const { auth } = NextAuth(authConfig);
import { 
    DEFAULT_LOGIN_REDIRECT,
    apiAuthPrefix,
    authRoutes,
    publicRoutes,
} from "./routes";


export default auth((req) => {
    const { nextUrl } = req;
    const isLoggedIn = !!req.auth;


    const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix);
    const isPublicRoute = publicRoutes.includes(nextUrl.pathname);
    const isAuthRoute = authRoutes.includes(nextUrl.pathname);


    if (isApiAuthRoute) {
        return;
    }

    if (isAuthRoute) {
        if (isLoggedIn) {
            return Response.redirect(new URL(DEFAULT_LOGIN_REDIRECT, nextUrl))
        }
        return;
    }


    if (!isLoggedIn && !isPublicRoute) {
        let callbackUrl = nextUrl.pathname;
        if (nextUrl.search) {
            callbackUrl += nextUrl.search
        }

        const encodedCallbackUrl = encodeURIComponent(callbackUrl)

        return Response.redirect(new URL(`/auth/login?callbackUrl=${encodedCallbackUrl}`, nextUrl));
    }

    return;
});
 

export const config = {
    matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
}

Here is my Middleware btw
I even tried removing all the code from this and debugging but the error is not occurring from here

81 Replies

@Asiatic Lion Hey guys, I don't know where this error is occurring from, i don't have any startsWith in my code When I click on a link for changing the URL I get an error : TypeError: Cannot read properties of undefined (reading 'startsWith') add-path-prefix.js:13 Uncaught TypeError: Cannot read properties of undefined (reading 'startsWith') ts import authConfig from "@/auth.config" import NextAuth from "next-auth" const { auth } = NextAuth(authConfig); import { DEFAULT_LOGIN_REDIRECT, apiAuthPrefix, authRoutes, publicRoutes, } from "./routes"; export default auth((req) => { const { nextUrl } = req; const isLoggedIn = !!req.auth; const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix); const isPublicRoute = publicRoutes.includes(nextUrl.pathname); const isAuthRoute = authRoutes.includes(nextUrl.pathname); if (isApiAuthRoute) { return; } if (isAuthRoute) { if (isLoggedIn) { return Response.redirect(new URL(DEFAULT_LOGIN_REDIRECT, nextUrl)) } return; } if (!isLoggedIn && !isPublicRoute) { let callbackUrl = nextUrl.pathname; if (nextUrl.search) { callbackUrl += nextUrl.search } const encodedCallbackUrl = encodeURIComponent(callbackUrl) return Response.redirect(new URL(`/auth/login?callbackUrl=${encodedCallbackUrl}`, nextUrl)); } return; }); export const config = { matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"], } Here is my Middleware btw I even tried removing all the code from this and debugging but the error is not occurring from here
help me which one is line 13 again?
can you send me the full error?
@ᴉuɐpɹɐɐ help me which one is line 13 again?
Asiatic LionOP
It's one of the Node-modules file
send me full error from the vscode console
@ᴉuɐpɹɐɐ send me full error from the vscode console
Asiatic LionOP
Nothing in vscode console
Unhandled Runtime Error
TypeError: Cannot read properties of undefined (reading 'startsWith')

Call Stack
startsWith
../src/shared/lib/router/utils/add-path-prefix.ts
path
../src/client/add-base-path.ts
href
../src/client/components/app-router.tsx
navigate
../src/client/components/app-router.tsx
React
options
../src/client/components/app-router.tsx
eval
node_modules\@react-aria\utils\dist\openLink.mjs (29:1)
$ea8dcbcb9ea1b556$var$getSyntheticLink
node_modules\@react-aria\utils\dist\openLink.mjs (88:1)
Object.open
node_modules\@react-aria\utils\dist\openLink.mjs (28:1)
onClick
node_modules\@nextui-org\use-aria-link\dist\index.mjs (42:1)
React
@ᴉuɐpɹɐɐ
you already ping me with the reply so no need to ping me again
share
../src/shared/lib/router/utils/add-path-prefix.ts
firstly read the error
add-path-prefix.js:13 Uncaught TypeError: Cannot read properties of undefined (reading 'startsWith')
is that ur code
and it seems to be app-router code
the router
@import_cubedev and it seems to be app-router code
Asiatic LionOP
It's in the node modules
lemme send the file code
add-path-prefix.ts is not your code?
Asiatic LionOP
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "addPathPrefix", {
    enumerable: true,
    get: function() {
        return addPathPrefix;
    }
});
const _parsepath = require("./parse-path");
function addPathPrefix(path, prefix) {
    if (!path.startsWith("/") || !prefix) {
        return path;
    }
    const { pathname, query, hash } = (0, _parsepath.parsePath)(path);
    return "" + prefix + pathname + query + hash;
}

//# sourceMappingURL=add-path-prefix.js.map
@ᴉuɐpɹɐɐ `add-path-prefix.ts` is not your code?
Asiatic LionOP
No
I even tried Reinstalling node_modules
also the router error can be if you set it up wrong
delete node_modules and run npm install
@import_cubedev delete node_modules and run npm install
Asiatic LionOP
I did this for like 100 times
@import_cubedev also the router error can be if you set it up wrong
Asiatic LionOP
I don't see any problem here
@Asiatic Lion Click to see attachment
ok but like try to backtrace the errors until you see the files you wrote
@ᴉuɐpɹɐɐ ok but like try to backtrace the errors until you see the files you wrote
Asiatic LionOP
There is no trace about the files i coded
nothing
@ᴉuɐpɹɐɐ seems like its from @react-aria. Do you use any of react-aria components?
Asiatic LionOP
um can this be the problem? :
          <Link href="#" aria-current="page" color="secondary">
            Customers
          </Link>
aria current
well you can try removing it?
@ᴉuɐpɹɐɐ well you can try removing it?
Asiatic LionOP
Didn't fixed it
@ᴉuɐpɹɐɐ seems like its from @react-aria. Do you use any of react-aria components?
Asiatic LionOP
Next-Ui uses React Aria
But it shouldn't be the problem
okay, lets see which one of the Next-UI is causing that error
assumptions are useless when troubleshooting, we need to see first what causes it before fixing it
(unless someone else knows why)
@ᴉuɐpɹɐɐ okay, lets see which one of the Next-UI is causing that error
Asiatic LionOP
Anywhere the link used
(NextUI link)
@Asiatic Lion Anywhere the link used
can you confirm that issue in a new project?
can you try a normal route like /
@ᴉuɐpɹɐɐ can you confirm that issue in a new project?
Asiatic LionOP
Nah it's working in other projects
@ᴉuɐpɹɐɐ your Link.. can you try not using `#` ?
Asiatic LionOP
Yeah but not working
does it still show error without #
@ᴉuɐpɹɐɐ does it still show error without `#`
Asiatic LionOP
Yeah
then it must be something else, keep searching 😭
back trace until the error is gone
Asiatic LionOP
I tried removing all the classnames and attributes from it
But not working
@ᴉuɐpɹɐɐ I noticed something
I tried using a <Link> inside the main page
It works well
Can problem be the components folder that starts without "_" the components when it's inside of the App directory>?
hm shouldnt be
where is that _components folder?
@Asiatic Lion Click to see attachment
Asiatic LionOP
.
didnt see _components in there
I explained it better
hmm you could try, but i dont think it matters
if it does, idk what is going on in NextUI's <Link> :Derp:
@ᴉuɐpɹɐɐ if it does, idk what is going on in NextUI's <Link> <:Derp:821045094488473710>
Asiatic LionOP
Everything is correct outside of dashboard folder
try reproducing it in a new project
and see if your hypothesis is correct
@ᴉuɐpɹɐɐ and see if your hypothesis is correct
Asiatic LionOP
I tried it outside of dashboard folder and it's working
ok try it in a new project
@ᴉuɐpɹɐɐ ok try it in a new project
Asiatic LionOP
You mean clone this or make a new project completely?
make new project, and make components folder inside app
basically try to confirm if it does cause the bug
@ᴉuɐpɹɐɐ make new project, and make components folder inside app
Asiatic LionOP
Or lemme try the components folder with _
@ᴉuɐpɹɐɐ basically try to confirm if it does cause the bug
Asiatic LionOP
Tried it
None of them works
Rlly don't know what to do
@ᴉuɐpɹɐɐ Nevermind
yes sometimes talking to a debugging duck helps
Asiatic LionOP
I'll use Nextjs default Link