Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEXT-671] Next.js 13.2.1 - Module '"next/navigation"' has no exported member 'usePathname'. #46360

Closed
1 task done
TadejPolajnar opened this issue Feb 24, 2023 · 28 comments
Closed
1 task done
Assignees
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team.

Comments

@TadejPolajnar
Copy link

TadejPolajnar commented Feb 24, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release1

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.3.0: Thu Jan  5 20:48:54 PST 2023; root:xnu-8792.81.2~2/RELEASE_ARM64_T6000
Binaries:
  Node: 16.15.1
  npm: 8.11.0
  Yarn: 1.22.19
  pnpm: 7.18.2
Relevant packages:
  next: 13.2.1
  eslint-config-next: 13.2.1
  react: 18.2.0
  react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue

https://stackblitz.com/edit/nextjs-q4nrme?file=package.json,tsconfig.json,pages%2Findex.tsx

To Reproduce

import { usePathname } from 'next/navigation';

export default function Home() {
  const pathname = usePathname();

  return <div>Pathname: {pathname}</div>;
}

Describe the Bug

Hook usePathname works as expected, it returns current path name however typescript throws an error which doesn't allow you to build the page.
Typescript error: Module '"next/navigation"' has no exported member 'usePathname'.

As a temporary solution I used // @ts-ignore to suppress the error for now.

Expected Behavior

Typescript type of usePathname needs be exported correctly and typescript error should disappear.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

From SyncLinear.com | NEXT-671

@TadejPolajnar TadejPolajnar added the bug Issue was opened via the bug report template. label Feb 24, 2023
@lionelrudaz
Copy link

lionelrudaz commented Feb 24, 2023

Same issue with useSearchParam.

Here's my code:

"use client";

import Link from 'next/link';
import { useSearchParams } from 'next/navigation';
import { CaretLeft } from 'phosphor-react';

type Props = {
  defaultUrl?: string
  style?: "flat" | "shadow"
}

const BackButton = ({
  defaultUrl,
  style
}: Props) => {
  const searchParams = useSearchParams();
  const returnUrl = searchParams.get('returnUrl') ?? defaultUrl;
  
  return (
    <Link
      href={`${returnUrl ? returnUrl : "/"}`}
      className={`mr-auto rounded-full p-2 flex items-center justify-center ${style === "shadow" ?  "bg-white shadow-md" : "bg-gray-200"}`}
      passHref
    >
      <CaretLeft size={24} color="black" weight="regular" />
    </Link>
  )
};

export default BackButton;

Was working with NextJS 13.1, not working anymore after the update to Next 13.2.

@TadejPolajnar how did you solve the error exactly? It doesn't work to add a // @ts-ignore in the beginning of the file.

I hope this helps.

@TadejPolajnar
Copy link
Author

TadejPolajnar commented Feb 24, 2023

@lionelrudaz you need to insert it right before the problematic row, here is an example:

import Image from 'next/image'
import { useMemo } from "react";
// @ts-ignore
import { usePathname } from 'next/navigation';

@lionelrudaz
Copy link

@TadejPolajnar it works as a temporary fix :)

@ooyay
Copy link

ooyay commented Feb 26, 2023

make sure navigation types is exist in next-env.d.ts

/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/navigation" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.

@TadejPolajnar
Copy link
Author

@ooyay next-env.d.ts changes every time you build the app or run development.

@TadejPolajnar
Copy link
Author

TadejPolajnar commented Feb 26, 2023

Based on a review I received on proposed PR, this is expected behavior. usePathname can only be used inside new app directory.

@ooyay
Copy link

ooyay commented Feb 26, 2023

@ooyay next-env.d.ts changes every time you build the app or run development.

I don't know, but the error was coming up until this line comes

@kevin-courbet
Copy link

I'm still getting this error in my App directory project... don't think this is fixed. 13.2.1

@AlanMorel
Copy link

I can also confirm that I'm also getting this issue when upgrading to 13.2.

My project is an app directory project as well. I don't think this issue should be closed.

@sabbircste
Copy link

Module parse failed: Unexpected token (6:5) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.

@kieranm
Copy link

kieranm commented Feb 27, 2023

Also seeing this issue in the app directory in 13.2. @TadejPolajnar I would suggest reopening.

@timneutkens timneutkens reopened this Feb 27, 2023
@timneutkens timneutkens added the linear: next Confirmed issue that is tracked by the Next.js team. label Feb 27, 2023
@timneutkens timneutkens changed the title Next.js 13.2.1 - Module '"next/navigation"' has no exported member 'usePathname'. [NEXT-671] Next.js 13.2.1 - Module '"next/navigation"' has no exported member 'usePathname'. Feb 27, 2023
aliemir added a commit to refinedev/refine that referenced this issue Feb 27, 2023
@balazsorban44
Copy link
Member

So it looks like the OP's issue is that they are trying to use next/navigation, while appDir: true is not enabled in the next.config.js file, as well as there is no app directory present. These conditions are the current requirement for us to correctly update the next-env.d.ts file for those types to be included.

@kieranm
Copy link

kieranm commented Feb 27, 2023

We are using the app folder and have appDir: true and ran into this issue. I think (I could be wrong) we needed to run next dev in order to have the types be updated. next dev isn't run in CI environments which is which is why our builds started failing after Dependabot tried to bump the version. Does this sound right?

@kieranm
Copy link

kieranm commented Feb 27, 2023

It seems like next build also adds the correct types so I can't really explain this. In any case it seems to have added the correct types now.

@Willem-Jaap
Copy link
Contributor

Also having this issue, indeed the issue seems to be that the next-env.d.ts is not updated in the linting proces.

42:11  Error: Unsafe assignment of an `any` value.  @typescript-eslint/no-unsafe-assignment
42:22  Error: Unsafe call of an `any` typed value.  @typescript-eslint/no-unsafe-call

@wyattjoh
Copy link
Member

In order to get the update to the types, you have to run next dev to generate updated next-env.d.ds file. Due to how TS type augmentation works, this was a neccesary evil. Once you run that, it should update it to include one of the following:

/// <reference types="next/navigation-types/navigation" />
/// <reference types="next/navigation-types/compat/navigation" />

This was changed in #45919 to ensure that users who are using both /pages and /app will have types representative of the hybrid use case.

@Willem-Jaap
Copy link
Contributor

Willem-Jaap commented Feb 27, 2023

I know this is the fix for development and actually works, but next dev is (and should obviously not be) used in a pipeline or github actions.
I have a pipeline which lints all code using 'next lint'. This validates all used types. If these types are not set correctly, It can't validate them, therefore the pipeline failes.

A possible solution is to also generate the file in a postinstall step, which would solve the problem.

@wyattjoh
Copy link
Member

Close this prematurely! Going to keep it open to see if we can smooth this or not.

@wyattjoh wyattjoh reopened this Feb 27, 2023
@giovanedann
Copy link

giovanedann commented Feb 27, 2023

Hey! im on 13.2.1 and even after running next dev my declaration file does not set these type declarations lines by default

I have already tried to remove node_modules and re-install packages, but the issue persists. Am i doing something wrong? Cuz i saw some comments that running the development server fixed the issue by adding those reference lines

Im not using the app directory, im using the pages directory, maybe is something related?

edit: i started a new project, and the declaration file is created with the correct references, seems like the problem is with the pages directory?

after running next dev this is the file generated:

/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.```

@balazsorban44
Copy link
Member

@kieranm

We are using the app folder and have appDir: true and ran into this issue. I think (I could be wrong) we needed to run next dev in order to have the types be updated. next dev isn't run in CI environments which is which is why our builds started failing after Dependabot tried to bump the version. Does this sound right?

It seems like next build also adds the correct types so I can't really explain this. In any case it seems to have added the correct types now.

Since both next dev and next build update types, this should not be an issue in CI or locally. 🤔

@Willem-Jaap

I know this is the fix for development and actually works, but next dev is (and should obviously not be) used in a pipeline or github actions.
I have a pipeline which lints all code using 'next lint'. This validates all used types. If these types are not set correctly, It can't validate them, therefore the pipeline failes.

Fixing this during linting is already tracked here #46104. (There is also a PR #46249)

@giovanedann

Im not using the app directory, im using the pages directory, maybe is something related?

Exactly. This type is generated only when appDir: true is set and you have an app directory. See my comment #46360 (comment)

@giovanedann
Copy link

Oh! i missed this comment, my bad!

So, the solution for that is to refactor the project to use the app directory or lock next in 13.1.6?

@wyattjoh
Copy link
Member

If you're not using app directory @giovanedann, then you should continue to use next/router, next/navigation is for use with projects utilizing app directory.

@giovanedann
Copy link

@wyattjoh sure, appreciate your time! Thank you very much! @balazsorban44

@jdouglass
Copy link

This might be a long shot here, but I get this exact same error message when I tried using turborepo in their monorepo configuration. I had ui components in a separate package folder, however, those ui components needed useSearchParams and usePathname. Is my only option as of right now is to use // @ts-ignore as a temporary fix or is there a better solution for my case?

@tgroutars
Copy link

Just want to add: This doesn't occur for me with next lint (afaik that command doesn't run type checks, just eslint?). But it does happen when I run type checks (tsc --noEmit) on github actions.

#46249 generates next-env.d.ts on next lint so it won't fix the issue unless next lint runs before typechecks.

Best way would be to have an explicit way to generate next-env.d.ts, either in postinstall or before running typechecks

@timinho111
Copy link

Also running into this issue when upgrading from next 13.1.6 to 13.2.1 without changing anything else.

/// <reference types="next/navigation-types/navigation" />

is set correctly after running next dev but it does not find useSearchParams() and usePathname().

Should next/navigation always been used when using the app directory or should next/router be used when using the router outside of the app directory?

aliemir added a commit to refinedev/refine that referenced this issue Feb 28, 2023
* feat(router-picker): add a context value for legacy and new router switch

* feat(routing): rename router to `legacy-router`

* feat(router): add new router context

* feat(router): update router bindings types

* feat(resource): update resource types in bindings

* refactor(resource): memoize values inside context

* feat(core): update refine wrapper with new router

* chore(querykeys): nullable id in detail

* chore(exports): update router exports as legacy

* chore(exports): export resource context

* test(mocks): update to legacy router

* feat(helpers): add legacy transform for resources

* chore(helpers): add resource sanitize helpers

* chore(pick-resource): add pick resource helper

* chore(redirect): handle undefined cases

* chore(route-generator): update legacy helper

* chore(router): add route matching and generation helpers

* feat(router): add new way of route matching helpers

* feat(react-router-v6): update bundle config for `/legacy` exports

* chore(react-router-v6): export legacy router from `/legacy` subpath

* feat(react-router-v6): add `<RefineRoutes />` component

* feat(react-router-v6): add and export bindings

* chore(use-navigation): update hook for backward compability and internal use

* feat(router): add new router hooks and deprecate legacy

* feat(router): update redirection logic in form

* feat(router): update use-resource with new bindings

* feat(router): update use breadcrumb for new bindings

* feat(use-table): update sync with location with new bindings

* chore(router): update hooks for undefined resources

* test(examples): add test setup for new router bindings

* chore(codemod): add router to legacy router codemod

* chore(router): add missing exports

* feat(codemod): add legacy router option

* test(examples): transform all to legacy router

* feat(ui-types): add `meta` to buttons

* chore(ui-types): remove deprecated access control prop

* feat(remix): add new bindings and move to legacy

* feat(react-router-v6): update bindings with components

* feat(nextjs): move existing routers to legacy

* feat(nextjs): add new router bindings for app and pages

* feat(core): add `link` to router bindings

* feat(core): add new useResource overload

* feat(core): update hooks with new bindings

* feat(core): add `useToPath` and `useLink` hooks

* feat(antd): update components with new router bindings

* feat(codemod): update legacy router transformation for all

* feat(codemod): add move-deprecated-access-control transformation

* feat(codemod): rename to legacy router provider prop

* test(inferred-params): add inferred params test

* test(core-router): add router helper unit tests

* refactor(core): update use-resource usage

* chore(core): remove deprecated props

* feat(antd): update with new routing

* feat(chakra): update with new routing

* feat(mantine): update with new routing

* feat(mui): update with new routing

* feat(hook-form): update with new routing

* feat(kbar): update with new routing

* chore(inferencer): update with deprecated values

* chore(core): remove warnings

* test(core): update test wrapper for routers

* feat(react-router-v6): add show segment

* feat(core): add show segment to default paths

* test(core): update hook tests for routers

* feat(core): update `useMenu`

* chore(core): update type export

* chore(core): update route composer

* feat(core): update auth hooks for new routing

* feat(core): add redirect with new router in authenticated component

* chore(core): fix backward compatibly

* test(core): fix auth hook tests

* chore(core): remove unused mocks

* chore(core): remove `react-router-dom` from devDeps

* fix(remix): move `qs` to deps

* feat(router): add unsaved-changes-notifier comps

* feat(core): deprecate layout props

* chore(core): replace params to meta

* feat(ui): add `meta` to breadcrumbs

* fix(core): update router bindings

* feat(core): create route on legacy even if not exists

* chore(core): replace type export

* test(core): transform resources at tests

* test(ui-tests): update router

* test(core): remove `fit`

* test(ui-tests): update button tests

* chore(core): allow deprecated options in meta

* fix(core): check for route in legacy

* fix(antd): update selected key in antd sider

* test(antd): update test setup

* fix(mui): selectedKey in sider

* fix(mui): fix broken show title

* fix(core): legacy resource can be undefined

* test(mui): update test setup

* fix(chakra-ui): variable name

* test(chakra-ui): update test setup

* fix(mantine): update sider key

* test(mantine): update test setup

* chore(core): fix lint error

* fix(core): update conflicting types

* fix(live-previews): rename to legacy

* chore(examples): remove obsolete props

* test(inferencer): update test setup

* test(inferencer): update snapshots

* test(ui-tests): fix clone button tests

* test(kbar): fix test setup

* chore(react-router-dom): upgrade version

* fix(react-router-v6): remove unused argument

* fix(react-router-v6): fix legacy looping

* chore(examples): remove unused import

* chore(examples): rename `routerProvider` prop

* chore(react-location): mark as private

* build: ignore refine-react-location at bootstrapping

* build(codemod): fix versions

* fix(codemod): update to matching versions

* build: remove codemod case

* chore(examples): fix undefined error

* chore(examples): fix import path

* chore(examples): update react-router imports

* chore(examples): update `Link`

* fix(codemod): add export rename for routers

* build(codemod): add `start` script

* chore(examples): fix export paths

* chore(examples): add fallback

* fix(core): use menu return type

* chore: exclude react-location

* chore(react-location): fix type error

* chore: update bootstrap script

* chore(examples): fix unknown prop name

* chore(examples): fix unexported import

* fix(core): useIsAuthenticated hook

* fix(core): `Authenticated` component

* test(core): update tests

* test(react-table): add resource

* test(antd): add route inference

* test(auth): fix test setups

* fix(core): handle undefined return in query function

* fix(react-router-v6): skip authentication if not provided

* refactor(core): move `patname` in bindings to response

* fix(core): authenticated component calls

* fix(core): do not include parent prefix in new routing system

* fix(core): use table link creation with new routing

* fix(core): always pick parent in breadcrumb

* fix(antd): use link by router type

* feat(sider): add `meta` to sider for menu creation

* chore(examples): add new routing example

* fix(core): allow additional nested params in parse

* feat(core): add form sync with location param type

* fix(core): remove console

* feat(antd): add `syncWithLocation` to `useDrawerForm`

* feat(antd): add `syncWithLocation` to `useModalForm`

* feat(mantine): add `syncWithLocation` to `useModalForm`

* feat(hook-form): add `syncWithLocation` to `useModalForm`

* chore(examples): note `routerBindings` as optional

* feat(remix): add `navigate-to-resource` component

* fix(nextjs): fix multiple calls in NavigateToResource

* refactor(syncWithLocation): use `identifier` when possible

* chore(examples): fix lint issue

* test(core): parent prefix on legacy and new

* chore(examples): remove commented authProvider

* chore(examples): fix wrong name

* chore(core): remove deprecation mark

* fix(react-router-v6): better workaround for usePrompt

* fix(core): remove repeated params

* fix(nextjs-router): type issue (vercel/next.js#46360)

* fix(nextjs): pathname issue

* fix(nextjs): wait for router to settle

* chore(live-previews): move demo components to legacy router

* chore(core): warn users for unknown properties in router provider

* chore: add changesets

* chore(core): add `identifier`

* chore(kbar): add changeset

* chore(examples): upgrade `with-nextjs-auth`

* chore(changeset): update changesets

* chore(examples): fix legacy type import

* chore(examples): fix build changes

* feat(live-previews): add react-router-dom

* chore(live-previews): add memory router replacement
@ijjk
Copy link
Member

ijjk commented Feb 28, 2023

Hi, this has been updated in the latest canary of Next.js, please update and give it a try!

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team.
Projects
None yet
Development

No branches or pull requests