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

aso*_*rov 22 typescript reactjs tsconfig yarnpkg next.js

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"
  ],
}
Run Code Online (Sandbox Code Playgroud)

Eri*_*ian 74

也许只需重新启动 TS 服务器就可以了。

\n

VsCode - Windows

\n

类型:ctrl + shift + p

\n

选择:> TypeScript: Restart TS server

\n

VsCode - Macbook:

\n

类型:\xe2\x87\xa7 + \xe2\x8c\x98 + P or F1

\n

选择:> TypeScript: Restart TS server

\n

  • 消失了2秒又回来了.. (5认同)
  • 就是这样 (2认同)

Vu *_*ong 52

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

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

在这里阅读其他编辑的信息

  • 谢谢,成功了 (7认同)
  • 我使用的是 Neovim,而不是 VS Code,但 [Yarn 有关编辑器 SDK 的文档](https://yarnpkg.com/getting-started/editor-sdks#neovim-native-lsp) 的链接正是我所需要的。 (3认同)

Vag*_*nes 17

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

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

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

完成所有 3 个步骤后,问题将得到解决。

  • @URogel Yarn PNP(即插即用)非常棒,它不使用node_modules文件夹,它具有快速的依赖安装,并且您使用的磁盘空间要少得多。但有必要确保所有项目依赖项都具有 PnP 支持。尽管我已经在某处读到 next.js 对 PnP 提供标准支持,但我意识到它并不总是有效。```nodeLinker: node-modules``` 确保重新创建node_module文件夹并且yarn 2/3的行为类似于yarn 1或npm。 (2认同)

yoy*_*joe 7

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

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

    \n
  2. \n
  3. 回顾/解决方案#2:我只是使用 引导一个新项目,然后使用 (using ) 或 rannpx create-next-app遵循 Next.js\'文档。我收到了与以下内容相关的错误npx create-next-app@latest --typescriptyarnyarn create next-app --typescriptts(2307): Cannot find module \'next/app\' or its corresponding type declarations.

    \n
  4. \n
  5. 根据@warfield\'s 的回答,我注意到由于某种原因我没有node_modules文件夹,即使在我运行后它也没有创建yarn。尽管我计划npm i使用.node_modulesyarn

    \n
  6. \n
\n

这是我运行时得到的日志yarn

\n
% yarn\n\xe2\x9e\xa4 YN0000: \xe2\x94\x8c Resolution step\n\xe2\x9e\xa4 YN0000: \xe2\x94\x94 Completed\n\xe2\x9e\xa4 YN0000: \xe2\x94\x8c Fetch step\n\xe2\x9e\xa4 YN0000: \xe2\x94\x94 Completed\n\xe2\x9e\xa4 YN0000: \xe2\x94\x8c Link step\n\xe2\x9e\xa4 YN0000: \xe2\x94\x82 ESM support for PnP uses the experimental loader API and is therefore experimental\n\xe2\x9e\xa4 YN0000: \xe2\x94\x94 Completed\n\xe2\x9e\xa4 YN0000: Done with warnings in 0s 244ms\n
Run Code Online (Sandbox Code Playgroud)\n

这是我运行时得到的日志npm i

\n
% npm i  \n\nadded 235 packages, and audited 236 packages in 10s\n\n78 packages are looking for funding\n  run `npm fund` for details\n\nfound 0 vulnerabilities\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
    \n
  1. 但出于好奇,我又跑了yarn一次,这就是结果输出。
  2. \n
\n
% yarn\n\xe2\x9e\xa4 YN0070: Migrating from Yarn 1; automatically enabling the compatibility node-modules linker \n\n\xe2\x9e\xa4 YN0000: \xe2\x94\x8c Resolution step\n\xe2\x9e\xa4 YN0000: \xe2\x94\x94 Completed in 3s 864ms\n\xe2\x9e\xa4 YN0000: \xe2\x94\x8c Fetch step\n\xe2\x9e\xa4 YN0013: \xe2\x94\x82 word-wrap@npm:1.2.3 can\'t be found in the cache and will be fetched from the remote registry\n\xe2\x9e\xa4 YN0013: \xe2\x94\x82 wrappy@npm:1.0.2 can\'t be found in the cache and will be fetched from the remote registry\n\xe2\x9e\xa4 YN0013: \xe2\x94\x82 yallist@npm:4.0.0 can\'t be found in the cache and will be fetched from the remote registry\n\xe2\x9e\xa4 YN0013: \xe2\x94\x82 yocto-queue@npm:0.1.0 can\'t be found in the cache and will be fetched from the remote registry\n\xe2\x9e\xa4 YN0019: \xe2\x94\x82 ms-npm-2.1.3-81ff3cfac1-aa92de6080.zip appears to be unused - removing\n\xe2\x9e\xa4 YN0000: \xe2\x94\x94 Completed in 0s 303ms\n\xe2\x9e\xa4 YN0000: \xe2\x94\x8c Link step\n\xe2\x9e\xa4 YN0007: \xe2\x94\x82 core-js-pure@npm:3.25.5 must be built because it never has been before or the last one failed\n\xe2\x9e\xa4 YN0000: \xe2\x94\x94 Completed in 4s 288ms\n\xe2\x9e\xa4 YN0000: Done in 8s 486ms\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 此外,如果您像我一样,您尝试tsconfig.json
  2. \n
\n
...\n  "include": [\n    "next-env.d.ts",\n    "**/*.ts",\n    "**/*.tsx"\n  ],\n...\n
Run Code Online (Sandbox Code Playgroud)\n

\n
...\n"include": [\n    "next-env.d.ts",\n    "pages/**/*",\n    "src/**/*",\n],\n...\n
Run Code Online (Sandbox Code Playgroud)\n

因为你认为基于这个 GitHub评论它会有帮助。您现在将看到一条新的错误消息ts(2686): \'React\' refers to a UMD global, but the current file is a module. Consider adding an import instead.。如果您只是执行了上述步骤,则无需执行此操作。所以我就把它改回来了。

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