繁体   English   中英

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

[英]Cannot find module 'next' or its corresponding type declarations

正在获取Cannot find module '' or its corresponding type declarations. 在 Next.js 项目中导入时。

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

纱线版本: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"
  ],
}

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

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

阅读更多

也许只需重新启动 TS 服务器即可。

类型: ctrl + shift + p

选择: > TypeScript: Restart TS server

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

  1. 只需重读@Vagnerlandio Nunes 的回答,就可以了。 问题解决了!

  2. 回顾/解决方案#2:我只是使用 npx npx create-next-app引导一个新项目,遵循 Next.js 的文档npx npx create-next-app@latest --typescript (使用yarn )或运行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 ,结果是 output。
% 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文件
...
  "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. 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 ,你应该会看到一些刷新正在进行.

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

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

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

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

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

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

意外删除 node_modules 文件夹是TS2307 Cannot find module ''错误的最简单解释。

OP评论也支持它

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

h/t @geisterfurz007 的评论。

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

"moduleResolution": "node",

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM