无法解析创建tsconfig.json后的模块。

huangapple go评论67阅读模式
英文:

Can't resolve modules after creating tsconfig.json

问题

我有一个运行良好的Next.js 13项目。

现在我想添加一个tsx组件。由于这需要TypeScript,我按照文档的建议执行了以下操作:

touch tsconfig.json

但在之后运行npm run dev时,开始出现找不到模块的错误:

$ npm run dev

> tailwindui-salient@0.1.0 dev
> next dev

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Loaded env from /Users/myuser/code/my-frontend/.env.development
info  - Loaded env from /Users/myuser/code/my-frontend/.env
We detected TypeScript in your project and created a tsconfig.json file for you.

error - ./src/pages/_app.jsx:4:0
Module not found: Can't resolve '@/styles/tailwind.css'
  2 | 
  3 | import 'focus-visible'
> 4 | import '@/styles/tailwind.css'
  5 | import { appWithTranslation } from 'next-i18next'
  6 | import { useEffect } from 'react'
  7 | import { init, push } from "@socialgouv/matomo-next"

https://nextjs.org/docs/messages/module-not-found

当我运行npm run build时,它抱怨有很多使用@/…语法的导入。

我的Node.js版本是v18.14.2。

编辑:以下是生成的tsconfig文件(这是由Next.js生成的,我没有更改任何内容):

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "incremental": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}
英文:

I have a next.js 13 project which runs all fine.

Now I wanted to add a tsx component. As this requires typescript I did as the docs said:

touch tsconfig.json

But when I run npm run dev after, I start getting module not found errors:

$ npm run dev        

> tailwindui-salient@0.1.0 dev
> next dev

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Loaded env from /Users/myuser/code/my-frontend/.env.development
info  - Loaded env from /Users/myuser/code/my-frontend/.env
We detected TypeScript in your project and created a tsconfig.json file for you.

error - ./src/pages/_app.jsx:4:0
Module not found: Can't resolve '@/styles/tailwind.css'
  2 | 
  3 | import 'focus-visible'
> 4 | import '@/styles/tailwind.css'
  5 | import { appWithTranslation } from 'next-i18next'
  6 | import { useEffect } from 'react'
  7 | import { init, push } from "@socialgouv/matomo-next";

https://nextjs.org/docs/messages/module-not-found

When I run npm run build it complains about many more imports with the @/… syntax.

My node version is v18.14.2.

Edit: Here is the generated tsconfig (this is generated by next.js, I haven't changed anything):

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "incremental": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

答案1

得分: 2

生成的 tsconfig.json 文件中缺少以下内容,应该添加到 compilerOptions 中:

"baseUrl": ".",
"paths": {
  "@/": ["src/*"]
}

我将这部分内容从 jsconfig.json 复制过来,不清楚为什么 Next.js 在生成 tsconfig 时没有添加它们。

英文:

The generated tsconfig.json was missing these lines within compilerOptions:

"baseUrl": ".",
 "paths": {
   "@/*": ["src/*"]
 }

I copied this over from jsconfig.json - no idea why next.js did not add them when it generated the tsconfig.

huangapple
  • 本文由 发表于 2023年3月7日 14:04:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定