ethanniser / next-typesafe-url

Fully typesafe, JSON serializable, and zod validated URL search params, dynamic route params, and routing for NextJS.
https://next-typesafe-url.dev
MIT License
362 stars 16 forks source link

Error when importing from "next-typesafe-url/pages" #66

Open Sturlen opened 1 year ago

Sturlen commented 1 year ago

Applies only to dynamic routes and specifically when using the hooks imported from "next-typesafe-url/pages". Static routes work like charm.

Full node error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'F:\Projects\next-url-repro\node_modules\next\router' imported from F:\Projects\next-url-repro\node_modules\next-typesafe-url\dist\pages.mjs   
Did you mean to import next-url-repro/node_modules/next/router.js?
    at new NodeError (node:internal/errors:393:5)
    at finalizeResolution (node:internal/modules/esm/resolve:305:11)
    at moduleResolve (node:internal/modules/esm/resolve:866:10)
    at defaultResolve (node:internal/modules/esm/resolve:1074:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:425:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36) {
  digest: undefined
}

Using create-t3-app. Happens when using npm and pnpm. Same result when running in CodeSandbox.

Repro of the issue

Available as github repo: https://github.com/Sturlen/next-url-repro

Steps

ethanniser commented 1 year ago

Thank you for reporting this bug, I am looking into it

ethanniser commented 1 year ago

im really confused

in next-typesafe-url/pages.mjs all its importing is

import { useRouter } from "next/router";

the file its trying to find exists image

and that exact same import works in a non library file:

import { type DynamicRoute } from "next-typesafe-url";
// import { useSearchParams } from "next-typesafe-url/pages"; //this import fails
import { useRouter } from "next/router";
import { z } from "zod";

const Route = {
  searchParams: z.object({
    postId: z.number().optional(),
  }),
} satisfies DynamicRoute;
export type RouteType = typeof Route;

export default function PostPage() {
  // const { data } = useSearchParams(Route.searchParams);
  // return <div>{data && <p>{data.postId ?? "none"}</p>}</div>;
  const router = useRouter();
  const { postId } = router.query;
  return <div>{postId && <p>{postId}</p>}</div>;
}

^^^ No Errors


also in the example apps in the main repo this problem does not exist

all very very strange

do you have any ideas?

Sturlen commented 1 year ago

Afraid I'm stumped by this as well 😅. Only idea I have is that might be related to the build output or module system doing something strange.

ericvoshall commented 7 months ago

Having the same issue - I was able to resolve it by adding transpilePackages: ['next-typesafe-url'] to my next.config.js.

ethanniser commented 7 months ago

thanks for sharing I still am puzzled by the issue, and why that fixes it but good to know it does