当涉及到构建我的下一个应用程序时,我在我的项目中遇到了这个反复出现的问题.
我的项目 struct 如下:
├── src
│ ├── app
│ │ ├── (buyer)
│ │ ├── (login)
│ │ ├── (vendor)
│ │ ├── api
│ │ ├── layout.tsx
│ │ ├── page.module.css
│ │ ├── page.tsx
│ │ └── providers.tsx
│ ├── constants
│ │ └── next-auth.ts
├── tailwind.config.js
├── tsconfig.json
└── tsconfig.spec.json
在构建这个应用程序时,我遇到了"找不到模块"的错误,主要是抱怨路径没有被解析.
例如:
Failed to compile.
../project-name/src/app/(buyer)/user/page.tsx:4:25
Type error: Cannot find module '@/constants/next-auth' or its corresponding type declarations.
2 | import { getServerSession } from 'next-auth'
3 |
> 4 | import { OPTIONS } from '@/constants/next-auth'
| ^
5 | import { Session } from 'next-auth/core/types'
6 |
7 | const User = async () => {
Error occurred while trying to run the npx next build
Error: Command failed: npx next build
编译器抱怨没有指向此模块的路径.让我们来看看这tsconfig.json
个:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"baseUrl": ".",
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"allowSyntheticDefaultImports": true,
"incremental": true,
"types": ["jest", "node"],
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"],
"@/app/*": ["./src/app/*"],
"@/constants/*": ["./src/constants/*"],
...
},
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
...
],
"exclude": [
"node_modules",
"jest.config.ts",
"src/**/*.{spec,test}.ts"
]
}
我知道我错过了什么,只是不知道在哪里.