当涉及到构建我的下一个应用程序时,我在我的项目中遇到了这个反复出现的问题.

我的项目 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"
    ]
}

我知道我错过了什么,只是不知道在哪里.

推荐答案

您只需在此处提供一条路径.

而不是

        "@/*": ["./src/*"],
        "@/app/*": ["./src/app/*"],
        "@/constants/*": ["./src/constants/*"],
        ...

只要有

"@/*": ["./src/*"]

还要确保您已经正确地定义了baseUrl.

Node.js相关问答推荐

MongoDB上的updateOne和findOneAndUpdate在Node.js中无法正常工作

在Node JS中获取控制台选项卡标题

Gmail API获取附件PDF未正确呈现.我遗漏了什么?

使用NodeJS的DynamoDB中的BatchGetItem出现MultipleValidationError

我的Node.js应用程序没有将Mongoose方法findByIdAndDelete作为函数进行检测

模块';"; node :流程&没有导出的成员';dlopen';

AWS-ROUTE 53指向S3存储桶,错误是别名目标名称不在目标区域内

在编译时强制不缩小类型范围

NPM如何管理node_modules传递依赖?

将文件传递到 AWS lambdas(nodejs) + API 网关后重新上传文件

使用 NPM 三个 mocha+typescript 进行测试

无法关闭 node.js 中的mongoose 连接

将 AllowDiskUse true 添加到 node.js 中的 MongoDB 聚合?

Sharp JS 依赖关系 destruct 了 Elastic Beanstalk 上的 Express Server

为什么我在生产环境中 deproy Next.js 示例项目时 CSS 不起作用?

为什么 req.params.id 返回 undefined未定义?

无服务器无法获取所有记录事件对象验证失败?

file.slim.js 中的苗条是什么

向 Stripe 提交付款请求时出现没有此类令牌错误

如何判断 Node.js 中是否设置了环境变量?