找不到模块“next/router”或其对应的类型声明

eh57zj3b  于 2023-04-20  发布在  其他
关注(0)|答案(1)|浏览(442)

目前我的NextJS应用程序有一些问题。
我得到下面的错误。这真的很奇怪,因为我的代码运行完美,没有错误。

import { useRouter } from 'next/router';
// Cannot find module 'next/router' or its corresponding type declarations.

然而,我在IDE中得到了上面的错误,这个错误停止了我的turbo build和yarn build命令的运行,导致我无法推向生产环境。
我已经重新启动我的ts服务器,卸载下一个,更新到最新版本,并运行yarn再次安装一切。
我在GitHub上做了一个示例仓库。https://github.com/MonkeeMan1/test
这将发生在我的所有下一个导入中。Cannot find module 'next/router' or its corresponding type declarations.ts(2307)Cannot find module 'next/link' or its corresponding type declarations.ts(2307)Cannot find module 'next/image' or its corresponding type declarations.ts(2307)
Package.json https://hatebin.com/ylujtdhtht
我尝试使用moduleResolution nodenode16
node导致我的本地包cachetesting找不到,所以无法导入。而node16导致nextjs导入错误。

vsikbqxv

vsikbqxv1#

问题出在你的tsconfig.json上。Next/React TS项目的配置不正确。
替换为=〉https://github.com/vercel/next.js/blob/canary/examples/hello-world/tsconfig.json
==解决方案==
阅读了你的评论后,我明白你想做得更好。
下面是我如何让它工作的。

第一步:修复nextjs.json的路径

apps/web/tsconfig.json

"extends": "../../packages/tsconfig/nextjs.json",

第二步:修复nextjs.json中的modulemoduleResolution

packages/tsconfig/nextjs.json

"module": "esnext",
"moduleResolution": "node",

第三步:修复cache: *匹配包名

apps/web/package.json

"cachetesting": "*",

第四步:将main添加到package.json中缓存

packages/cache/package.json

"main": "dist/index.js",

不知道你是否看过this link,但这有助于我调试

相关问题