英文:
"Cannot find module" errors despite correct paths provided in tsconfig
问题
我在我的项目中遇到了一个反复出现的问题,涉及到构建我的下一个应用程序时。
我的项目结构如下:
├── 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"
]
}
我知道我漏掉了一些东西,只是不知道在哪里。
英文:
I'm running into this recurring issue in my project when it comes to building my next app.
My project structure is the following:
├── 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
When building this app, I'm running into "cannot find module" errors, mostly complaining about paths not being resolved.
For example:
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
The compiler is complaining that there is no path pointing to this module. Let's check the 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"
]
}
I know I'm missing something, I just don't know where.
答案1
得分: 2
只需提供一个路径。
而不是
"@/*": ["./src/*"],
"@/app/*": ["./src/app/*"],
"@/constants/*": ["./src/constants/*"],
...
只需
"@/*": ["./src/*"]
还要确保已正确定义了baseUrl
。
英文:
You just need to provide one path here.
instead of
"@/*": ["./src/*"],
"@/app/*": ["./src/app/*"],
"@/constants/*": ["./src/constants/*"],
...
just have
"@/*": ["./src/*"]
Also make sure that you have defined baseUrl
properly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论