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

问题描述 投票:0回答:9

在 Next.js 项目中导入时获得

Cannot find module '' or its corresponding type declarations.

每次导入都会发生这种情况。 Preview

纱线版本:3.1.0-rc.2
下一版本:11.1.2

tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "importHelpers": true,
    "jsx": "preserve",
    // "baseUrl": "src"
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ],
}
reactjs typescript next.js yarnpkg tsconfig
9个回答
62
投票

也许重启TS服务器就可以了。

VsCode - Windows

类型:

ctrl + shift + p

选择:

> TypeScript: Restart TS server

VsCode - Macbook:

类型:

⇧ + ⌘ + P or F1

选择:

> TypeScript: Restart TS server


48
投票

如果您使用的是yarn 3和VSCode,那么您需要按照以下步骤设置编辑器:

  • 奔跑
    yarn dlx @yarnpkg/sdks vscode
  • 打开任意
    TypeScript
    文件
  • ctrl+shift+p
  • 选择“选择 TypeScript 版本”
  • 选择“使用工作区版本”

请阅读此处其他编辑者


15
投票

我尝试了 Cuong Vu 和其他许多人建议的解决方案,但没有成功。对我来说有效的是以下解决方案:

在 Yarn 2/3 中,由于 PnP,删除了对 node_modules 文件夹中模块的支持。请按照以下步骤解决问题。

  1. 在项目根目录中创建一个
    .yarnrc.yml
    文件,
  2. 插入文件
    nodeLinker: node-modules
  3. 在终端中运行命令
    yarn
    以重新创建node_modules文件夹。

按照所有3个步骤,问题将得到解决。


6
投票

我想为可能欣赏它们的其他人提供更多背景信息和附加注释。

  1. 刚刚重读了@Vagnerlandio Nunes 的答案,它是正确的。问题解决了!

  2. 回顾/解决方案#2:我只是使用

    npx create-next-app
    引导一个新项目,然后使用npx create-next-app@latest --typescript(使用
    yarn
    )遵循Next.js的
    文档
    或运行
    yarn create next-app --typescript
    。我收到与
    ts(2307): Cannot find module 'next/app' or its corresponding type declarations.

    有关的错误
  3. 根据@warfield的回答,我注意到由于某种原因我没有

    node_modules
    文件夹,甚至在我运行
    yarn
    之后它也没有创建。我必须运行
    npm i
    来创建
    node_modules
    文件夹,即使我计划使用
    yarn

这是我运行时得到的日志

yarn

% yarn
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: │ ESM support for PnP uses the experimental loader API and is therefore experimental
➤ YN0000: └ Completed
➤ YN0000: Done with warnings in 0s 244ms

这是我运行时得到的日志

npm i

% npm i  

added 235 packages, and audited 236 packages in 10s

78 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

现在问题解决了,错误消息也消失了!

  1. 但是出于好奇,我再次运行了
    yarn
    ,这就是结果输出。
% yarn
➤ YN0070: Migrating from Yarn 1; automatically enabling the compatibility node-modules linker 👍

➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed in 3s 864ms
➤ YN0000: ┌ Fetch step
➤ YN0013: │ word-wrap@npm:1.2.3 can't be found in the cache and will be fetched from the remote registry
➤ YN0013: │ wrappy@npm:1.0.2 can't be found in the cache and will be fetched from the remote registry
➤ YN0013: │ yallist@npm:4.0.0 can't be found in the cache and will be fetched from the remote registry
➤ YN0013: │ yocto-queue@npm:0.1.0 can't be found in the cache and will be fetched from the remote registry
➤ YN0019: │ ms-npm-2.1.3-81ff3cfac1-aa92de6080.zip appears to be unused - removing
➤ YN0000: └ Completed in 0s 303ms
➤ YN0000: ┌ Link step
➤ YN0007: │ core-js-pure@npm:3.25.5 must be built because it never has been before or the last one failed
➤ YN0000: └ Completed in 4s 288ms
➤ YN0000: Done in 8s 486ms
  1. 此外,如果您像我一样,您尝试过从
     修改您的 
    tsconfig.json
  2. 文件
...
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
...

...
"include": [
    "next-env.d.ts",
    "pages/**/*",
    "src/**/*",
],
...

因为你认为基于此 GitHub 评论它会有所帮助。您现在会看到一条新的错误消息

ts(2686): 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.
。如果您只是执行了上述步骤,则无需执行此操作。所以我就把它改回来了。

  1. 也无需将
    tsconfig.json
    "compilerOptions": { ... }
    "jsx": "preserve"
    更改为
    "jsx": "react-jsx"
    参考另一个尝试的解决方案)。
  1. 在任何时候,如果您想确保所做的更改(关于 tsconfig)已生效,请尝试 command + shift + p 并输入
    > TypeScript: Restart TS server
    ,您应该会看到一些令人耳目一新的情况.

4
投票

在尝试其他解决方案之前:

  1. 仔细检查您是否有一个
    node_modules
    文件夹
  2. 如果没有,请运行
    yarn
    (或
    npm i
    )来(重新)安装模块

意外删除node_modules文件夹是对

TS2307 Cannot find module ''
错误的最简单解释。

也得到OP评论的支持

“我没有node_module文件夹。只有.yarn和yarn.lock”

h/t @geisterfurz007 的评论。


0
投票

我将其添加到我的 tsconfig.json 文件中 我可以看到你已经有了它,但它不是我的解决方案的一部分

"moduleResolution": "node",

0
投票

根据上述答案,我尝试按照https://yarnpkg.com/getting-started/editor-sdks

配置我的编辑器

我重新启动,问题仍然存在

最后我降级了纱线,解决了我的问题

yarn set version classic && yarn install

现在我有了node_modules并且vscode很高兴

我也更喜欢有源代码可供浏览 - 显然 vscode 应该能够访问源代码但这对我来说也不起作用(而且我喜欢搜索原始文件)


0
投票

在我的情况下,步骤如下

worked(MacOs & VSCode)

步骤 1.

command + shift + p

步骤 2. 搜索 >

TypeScript: Restart TS server
并按 enter


0
投票

对于我的解决方案,这只是一个用户错误。我创建了新的别名,但它们需要按键上的星号。

所以这个:

"paths": {
      ...,
      "@lib/": ["./app/lib/*"],
    },

变成这样:

"paths": {
  ...,
  "@lib/*": ["./app/lib/*"],
},
© www.soinside.com 2019 - 2024. All rights reserved.