【发布时间】:2022-02-17 17:47:23
【问题描述】:
我收到以下错误,尝试从 gatsby-plugin-dark-mode 导入 ThemeToggler
module "C:/Users/Karim Elnemr/Desktop/my-gatsby-website/node_modules/gatsby-plugin-dark-mode/index"
Could not find a declaration file for module 'gatsby-plugin-dark-mode'. 'C:/Users/Karim Elnemr/Desktop/my-gatsby-website/node_modules/gatsby-plugin-dark-mode/index.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/gatsby-plugin-dark-mode` if it exists or add a new declaration (.d.ts) file containing `declare module 'gatsby-plugin-dark-mode';`ts(7016)
我也一直在终端上遇到这个错误,我尝试安装插件
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: gatsby-starter-hello-world@0.1.0
npm ERR! Found: react@17.0.2
npm ERR! node_modules/react
npm ERR! react@"^17.0.1" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"16.x" from gatsby-plugin-dark-mode@1.1.2
npm ERR! node_modules/gatsby-plugin-dark-mode
npm ERR! gatsby-plugin-dark-mode@"^1.1.2" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
下面是我导入上述插件的地方
import React from "react"
import { ThemeToggler } from "gatsby-plugin-dark-mode"
export default function ThemeToggle() {
return (
<ThemeToggler>
{({ theme, toggleTheme }) => {
if (theme == null) {
return null
}
return (
<label className="theme-switch">
<input
type="checkbox"
onChange={e => toggleTheme(e.target.checked ? "dark" : "light")}
checked={theme === "dark"}
/>
<span className="toggle"></span>
</label>
)
}}
</ThemeToggler>
)
}
需要一些解释这意味着什么以及为什么会发生,以便我可以解决它。
【问题讨论】:
-
你正在为你的 react 项目使用 typescript 模板?
-
不,我没有使用任何打字稿!
-
很奇怪,通常会在 typescript 中询问项目附带的类型,您是否尝试过 "npm i --save-dev @types/gatsby-plugin-dark-mode"
-
真的很奇怪,请在使用导入库时分享文件,我还在想某处有打字稿
-
尝试将 react 版本降级到 16.14.0 并执行 npm install --force 并且不要忘记 react-dom