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"
  ],
}
Eri*_*ian 74
也许只需重新启动 TS 服务器就可以了。
\nVsCode - Windows
\n类型:ctrl + shift + p
选择:> TypeScript: Restart TS server
VsCode - Macbook:
\n类型:\xe2\x87\xa7 + \xe2\x8c\x98 + P or F1
选择:> TypeScript: Restart TS server
Vu *_*ong 52
如果您使用的是yarn 3和VSCode,那么您需要按照以下步骤来设置编辑器:
yarn dlx @yarnpkg/sdks vscodeTypeScript文件ctrl+shift+p在这里阅读其他编辑的信息
Vag*_*nes 17
我尝试了 Cuong Vu 和其他许多人建议的解决方案,但没有成功。对我来说有效的是以下解决方案:
在 Yarn 2/3 中,由于 PnP,删除了对 node_modules 文件夹中模块的支持。请按照以下步骤解决该问题。
.yarnrc.yml在项目根目录中创建一个文件,nodeLinker: node-modules。yarn在终端中运行命令以重新创建 node_modules 文件夹。完成所有 3 个步骤后,问题将得到解决。
我想为可能欣赏它们的其他人提供更多背景信息和附加注释。
\n刚刚重读了@Vagnerlandio Nunes 的答案,它是正确的。问题解决了!
\n回顾/解决方案#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.
根据@warfield\'s 的回答,我注意到由于某种原因我没有node_modules文件夹,即使在我运行后它也没有创建yarn。尽管我计划npm i使用.node_modulesyarn
这是我运行时得到的日志yarn。
% 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这是我运行时得到的日志npm i。
% 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现在问题已经解决了,错误消息也消失了!
\nyarn一次,这就是结果输出。% 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\ntsconfig.json从...\n  "include": [\n    "next-env.d.ts",\n    "**/*.ts",\n    "**/*.tsx"\n  ],\n...\n到
\n...\n"include": [\n    "next-env.d.ts",\n    "pages/**/*",\n    "src/**/*",\n],\n...\n因为你认为基于这个 GitHub评论它会有帮助。您现在将看到一条新的错误消息ts(2686): \'React\' refers to a UMD global, but the current file is a module. Consider adding an import instead.。如果您只是执行了上述步骤,则无需执行此操作。所以我就把它改回来了。
tsconfig.json另一个尝试"compilerOptions": { ... }的解决方案)。"jsx": "preserve""jsx": "react-jsx"> TypeScript: Restart TS server,您应该会看到一些令人耳目一新的情况。| 归档时间: | 
 | 
| 查看次数: | 52012 次 | 
| 最近记录: |