![](/img/trans.png)
[英]Type error: cannot find module '...' or its corresponding type declarations
[英]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
也许只需重新启动 TS 服务器即可。
类型: ctrl + shift + p
选择: > TypeScript: Restart TS server
我想为可能欣赏它们的其他人提供更多背景信息和附加说明。
只需重读@Vagnerlandio Nunes 的回答,就可以了。 问题解决了!
回顾/解决方案#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.
通过@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
现在问题解决了,错误信息也消失了!
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
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.
. 如果您只是执行上述步骤,则没有必要这样做。 所以我只是把它改回来了。
tsconfig.json
的"compilerOptions": {... }
从"jsx": "preserve"
更改为"jsx": "react-jsx"
( 参考另一个尝试的解决方案)。> TypeScript: Restart TS server
,你应该会看到一些刷新正在进行.我尝试了 Cuong Vu 和许多其他人建议的解决方案,但没有奏效。 对我来说有效的是以下解决方案:
在 Yarn 2/3 中,由于 PnP,删除了对 node_modules 文件夹中模块的支持。 请按照以下步骤解决问题。
.yarnrc.yml
文件,nodeLinker: node-modules
。yarn
以重新创建 node_modules 文件夹。按照所有3个步骤,问题将得到解决。
在尝试其他解决方案之前:
node_modules
文件夹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.