gatsby-plugin-mdx 找不到模块 'gatsby/webpack'

gatsby-plugin-mdx cannot find module 'gatsby/webpack'

我刚刚将 gatsby-plugin-mdx 添加到我的 gatsby 插件中,但我在启动项目时一直收到此错误。

Error in "/Gatsby/node_modules/gatsby-plugin-mdx/gatsby-node.js": 
Cannot find module 'gatsby/webpack'

我确实注意到有一些版本警告,如下所示,但我不确定这是否是问题所在,因为只要没有将 gatsby-plugin-mdx 添加到 gatsby-config 中,它就可以工作。
我是 gatsby 的新手,添加的大部分插件都是按照他们的教程添加的,所以我真的在这里摸不着头脑。
知道是什么原因造成的以及如何解决吗?

warn Plugin gatsby-plugin-sass is not compatible with your 
gatsby version 3.14.6 - It requires gatsby@^4.0.0-next

这是我的 package.json

{
  "name": "gatsby",
  "version": "1.0.0",
  "private": true,
  "description": "Gatsby",
  "author": "V",
  "keywords": [
    "gatsby"
  ],
  "scripts": {
    "develop": "gatsby develop",
    "start": "gatsby develop",
    "build": "gatsby build",
    "serve": "gatsby serve",
    "clean": "gatsby clean"
  },
  "dependencies": {
    "@mdx-js/mdx": "^1.6.22",
    "@mdx-js/react": "^1.6.22",
    "@react-three/cannon": "^4.0.1",
    "@react-three/drei": "^7.22.1",
    "@react-three/fiber": "^7.0.19",
    "classnames": "^2.3.1",
    "dayjs": "^1.10.7",
    "framer-motion": "^5.3.0",
    "gatsby": "^3.12.1",
    "gatsby-plugin-gatsby-cloud": "^3.0.0",
    "gatsby-plugin-image": "^1.12.0",
    "gatsby-plugin-layout": "^3.2.0",
    "gatsby-plugin-mdx": "^3.3.0",
    "gatsby-plugin-sass": "^5.1.1",
    "gatsby-plugin-sharp": "^3.12.0",
    "gatsby-source-filesystem": "^4.3.0",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "sass": "^1.43.4",
    "three": "^0.134.0"
  },
  "devDependencies": {}
}

还有我的盖茨比-config.js

module.exports = {
  siteMetadata: {
    siteUrl: "https://www.yourdomain.tld",
    title: "Gatsby",
  },
  plugins: [
    "gatsby-plugin-gatsby-cloud",
    "gatsby-plugin-image",
    "gatsby-plugin-sharp",
    `gatsby-plugin-sass`,
    "gatsby-plugin-layout",
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: `src`,
        path: `${__dirname}/src/works/`,
      },
    },
    "gatsby-plugin-mdx" // adding this creates error!
  ],
};

如果没有看到该网站,很难发现,但我猜潜在的问题是 gatsby-plugin-mdx 版本 (^3.3.0)。根据 changelog, the release note is linked to Gatsby v4.3,它与您当前的 Gatsby 不兼容,因为您的是 3.14.6,它需要重大升级 (Gatsby 4)。

也就是说,您有两个选择:

  • 升级 Gatsby 和所有相关依赖项至少到版本 4
  • gatsby-plugin-mdx 降级为 2.14.0,这显然与您当前的 Gatsby 版本兼容。

在这两种情况下,您都需要删除 package-lock.json(或 yarn.lock)并将软件包重新安装到所需的版本。