在 Next.js 项目中导入时获得
Cannot find module '' or its corresponding type declarations.
。
纱线版本: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"
],
}
也许重启TS服务器就可以了。
VsCode - Windows
类型:
ctrl + shift + p
选择:
> TypeScript: Restart TS server
VsCode - Macbook:
类型:
⇧ + ⌘ + P or F1
选择:
> TypeScript: Restart TS server
如果您使用的是yarn 3和VSCode,那么您需要按照以下步骤设置编辑器:
yarn dlx @yarnpkg/sdks vscode
TypeScript
文件ctrl+shift+p
请阅读此处其他编辑者
我尝试了 Cuong Vu 和其他许多人建议的解决方案,但没有成功。对我来说有效的是以下解决方案:
在 Yarn 2/3 中,由于 PnP,删除了对 node_modules 文件夹中模块的支持。请按照以下步骤解决问题。
.yarnrc.yml
文件,nodeLinker: node-modules
。yarn
以重新创建node_modules文件夹。按照所有3个步骤,问题将得到解决。
我想为可能欣赏它们的其他人提供更多背景信息和附加注释。
刚刚重读了@Vagnerlandio Nunes 的答案,它是正确的。问题解决了!
回顾/解决方案#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.
有关的错误
根据@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
,这就是结果输出。% 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.
。如果您只是执行了上述步骤,则无需执行此操作。所以我就把它改回来了。
tsconfig.json
的 "compilerOptions": { ... }
从 "jsx": "preserve"
更改为 "jsx": "react-jsx"
(参考另一个尝试的解决方案)。> TypeScript: Restart TS server
,您应该会看到一些令人耳目一新的情况.在尝试其他解决方案之前:
node_modules
文件夹yarn
(或 npm i
)来(重新)安装模块意外删除node_modules文件夹是对
TS2307 Cannot find module ''
错误的最简单解释。
也得到OP评论的支持
“我没有node_module文件夹。只有.yarn和yarn.lock”
h/t @geisterfurz007 的评论。
我将其添加到我的 tsconfig.json 文件中 我可以看到你已经有了它,但它不是我的解决方案的一部分
"moduleResolution": "node",
根据上述答案,我尝试按照https://yarnpkg.com/getting-started/editor-sdks
配置我的编辑器我重新启动,问题仍然存在
最后我降级了纱线,解决了我的问题
yarn set version classic && yarn install
现在我有了node_modules并且vscode很高兴
我也更喜欢有源代码可供浏览 - 显然 vscode 应该能够访问源代码但这对我来说也不起作用(而且我喜欢搜索原始文件)
在我的情况下,步骤如下
worked(MacOs & VSCode)
步骤 1.
command + shift + p
和
步骤 2. 搜索 >
TypeScript: Restart TS server
并按 enter
对于我的解决方案,这只是一个用户错误。我创建了新的别名,但它们需要按键上的星号。
所以这个:
"paths": {
...,
"@lib/": ["./app/lib/*"],
},
变成这样:
"paths": {
...,
"@lib/*": ["./app/lib/*"],
},