【问题标题】:Could not find a declaration file for module 'gatsby-plugin-dark-mode'找不到模块“gatsby-plugin-dark-mode”的声明文件
【发布时间】: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

标签: node.js reactjs npm


【解决方案1】:

安装时的错误意味着你要安装的库依赖于 react 16,而你的项目中安装了 React 17

你可以使用这个命令 npm i gatsby-plugin-dark-mode --legacy-peer-deps

【讨论】:

  • 我试过这个选项也仍然给我找不到模块'gatsby-plugin-dark-mode'的声明文件。
  • 正常@types/gatsby-plugin-dark-mode 不存在模块不支持打字稿
  • 我使用 "react": "^17.0.1"
  • 您在项目中使用打字稿?您可以将 webpack 配置添加到问题中吗?
【解决方案2】:

您需要降级您的反应版本: 在你的 package.json 中

   "dependencies": {
   ...
   "react": "16.14.0",
    "react-dom": "16.14.0",
    ...
    }

然后删除你的 node-modules 文件夹 并运行

yarn install or npm install

【讨论】:

    【解决方案3】:

    您可以这样做:npm i gatsby-plugin-dark-mode --force

    【讨论】:

    • 我试过了,还是给我找不到模块'gatsby-plugin-dark-mode'的声明文件。
    猜你喜欢
    • 2022-08-15
    • 2022-01-11
    • 2017-08-13
    • 1970-01-01
    • 2021-01-02
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多