首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用npm安装软件包时出错

使用npm安装软件包时出错
EN

Stack Overflow用户
提问于 2022-06-18 01:49:33
回答 3查看 1K关注 0票数 1

我试图添加一个博客到我的盖茨比网站,其中涉及增加MDX的支持。当我试图通过运行以下命令安装MDX插件时,我会得到以下错误:

命令 npm install gatsby-plugin-mdx @mdx-js/mdx@v1 @mdx-js/react@v1

误差

代码语言:javascript
复制
E:\Dev\Web\dantcho.com>npm install gatsby-plugin-mdx @mdx-js/mdx@v1 @mdx-js/react@v1
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: dantcho@1.0.0
npm ERR! Found: react@18.2.0
npm ERR! node_modules/react
npm ERR!   react@"^18.2.0" from the root project
npm ERR!   peer react@"^16.9.0 || ^17.0.0 || ^18.0.0" from gatsby-plugin-mdx@3.16.1
npm ERR!   node_modules/gatsby-plugin-mdx
npm ERR!     gatsby-plugin-mdx@"*" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.13.1 || ^17.0.0" from @mdx-js/react@1.6.22
npm ERR! node_modules/@mdx-js/react
npm ERR!   @mdx-js/react@"v1" from the root project
npm ERR!   peer @mdx-js/react@"^1.0.0" from gatsby-plugin-mdx@3.16.1
npm ERR!   node_modules/gatsby-plugin-mdx
npm ERR!     gatsby-plugin-mdx@"*" 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!
npm ERR! See C:\Users\dantc\AppData\Local\npm-cache\eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\dantc\AppData\Local\npm-cache\_logs\2022-06-18T01_43_31_526Z-debug-0.log

附加信息:

package.json

代码语言:javascript
复制
{
  "name": "dantcho",
  "version": "1.0.0",
  "private": true,
  "description": "Dantcho",
  "author": "Yordan Hristov (Dantcho)",
  "keywords": [
    "gatsby"
  ],
  "scripts": {
    "develop": "gatsby develop",
    "start": "gatsby develop",
    "build": "gatsby build",
    "serve": "gatsby serve",
    "clean": "gatsby clean"
  },
  "dependencies": {
    "@fontsource/rubik": "^4.5.10",
    "babel-plugin-styled-components": "^2.0.7",
    "framer-motion": "^6.3.11",
    "gatsby": "^4.16.0",
    "gatsby-plugin-image": "^2.16.1",
    "gatsby-plugin-manifest": "^4.16.0",
    "gatsby-plugin-react-helmet": "^5.16.0",
    "gatsby-plugin-sharp": "^4.16.1",
    "gatsby-plugin-styled-components": "^5.16.0",
    "gatsby-remark-images": "^6.16.0",
    "gatsby-source-filesystem": "^4.16.0",
    "gatsby-transformer-remark": "^5.16.0",
    "gatsby-transformer-sharp": "^4.16.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-helmet": "^6.1.0",
    "styled-components": "^5.3.5"
  }
}

节点版本 16.14.0

NPM版本 8.6.0

OS Windows 10

更新

我试图创建一个全新的盖茨比项目,并安装我需要的软件包。这样,我就可以消除不同软件包的旧版本或错误版本的任何可能性。(这是行不通的)我开始有一个类似的问题,与许多其他软件包。

解决方案(至少对我而言)

转而使用雅恩。

EN

回答 3

Stack Overflow用户

发布于 2022-06-21 13:49:59

我试过这个命令,它对我来说很好。

代码语言:javascript
复制
npm config set legacy-peer-deps true

npm install  --force
票数 3
EN

Stack Overflow用户

发布于 2022-06-27 22:19:01

在尝试安装时,我也犯了同样的错误。项目中的React (18.2.0)和这些插件应该使用的.之间的依赖冲突。

因此,解决方案非常简单:只需通过编辑package.json文件来降低您的react。

而不是这样:

代码语言:javascript
复制
"dependencies": {
  ...
  "react": "^18.2.0",
  "react-dom": "^18.2.0",
  ...
},

更改为早期版本,如16.14.0:

代码语言:javascript
复制
"dependencies": {
  ...
  "react": "^16.14.0",
  "react-dom": "^16.14.0",
  ...
},

然后,保存文件。之后,在终端中运行以下代码以删除安装在项目中的当前模块。

rm node_modules

最后,只需通过运行以下命令重新安装node_modules包:

代码语言:javascript
复制
npm install

现在,您可以重试并安装您的mdx插件:

代码语言:javascript
复制
npm install gatsby-plugin-mdx @mdx-js/mdx@v1 @mdx-js/react@v1

希望它对你有用!

(我不太确定你的反应是否会引起一些副作用问题,但这种方式对我来说是有效的。如果有人能澄清这件事,我会很感激的)

票数 2
EN

Stack Overflow用户

发布于 2022-06-18 08:29:46

我试过这个命令npm install gatsby-plugin-mdx @mdx-js/mdx@v1 @mdx-js/react@v1,它对我很好

package.json

代码语言:javascript
复制
  "name": "my-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@mdx-js/mdx": "^1.6.22",
    "@mdx-js/react": "^1.6.22",
    "gatsby-plugin-mdx": "^3.16.1"
  }
}

我认为问题在文件夹路径中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72666146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档