如何让 npm 安装单个包?

How to get npm to install a single package?

我下载了一个 npm 项目,npm install 失败并出现一堆依赖错误。为了找出问题所在,我尝试使用 npm install react 等单独安装每个依赖项,但该命令忽略了单个参数,而是给出了与尝试安装整个项目相同的错误。无奈之下我尝试了https://docs.npmjs.com/cli/v8/commands/npm-install的例子:'npm install sax`,其中sax与项目无关,仍然失败。

package.json 是:

{
  "name": "graphiql-explorer-example",
  "homepage": "https://onegraph.github.io/graphiql-explorer-example",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "graphiql": "^0.12.0",
    "graphql": "^14.1.1",
    "graphiql-explorer": "^0.6.2",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-scripts": "2.1.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "flow": "flow",
    "deploy": "gh-pages -d build",
    "predeploy": "yarn build",
    "prettier": "prettier --write \"src/**/*.js\" \"docs/**/*.md\" README.md"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "flow-bin": "^0.91.0",
    "gh-pages": "^2.0.1"
  },
  "description": "Example usage of [OneGraph](https://www.onegraph.com)'s open source [GraphiQL explorer](https://github.com/OneGraph/graphiql-explorer).",
  "main": "index.js",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/OneGraph/graphiql-explorer-example.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/OneGraph/graphiql-explorer-example/issues"
  }
}

错误信息是:

   > npm install react

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: graphiql-explorer-example@0.1.0
npm ERR! Found: graphql@14.1.1
npm ERR! node_modules/graphql
npm ERR!   graphql@"^14.1.1" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer graphql@"^0.6.0 || ^0.7.0 || ^0.8.0-b || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0" from graphiql@0.12.0
npm ERR! node_modules/graphiql
npm ERR!   graphiql@"^0.12.0" 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.

日志文件(不包括一堆“定时”和“愚蠢”的消息)是:


1 info using npm@8.8.0
2 info using node@v16.14.2

22 verbose title npm install sax
23 verbose argv "install" "sax"

41 http fetch GET 200 https://registry.npmjs.org/graphiql 144ms (cache revalidated)

43 http fetch GET 200 https://registry.npmjs.org/graphql 47ms (cache revalidated)

47 verbose stack Error: unable to resolve dependency tree

如您所见,npm 在第 23 行正确解析了“sax”参数,但随后忽略它并尝试安装 graphiql。

如何让 npm 安装单个包,例如最新版本的 react,而不是尝试安装 graphiql-explorer 的所有依赖项?

已修复 运行 宁 npm install 命令。

$ npm install

根据您运行安装命令时的错误信息:

npm ERR! ERESOLVE unable to resolve dependency tree

它声明某些依赖项与其他依赖项不兼容。要解决此问题,您需要 运行 带有以下标志的安装命令。

$ npm install --legacy-peer-deps

这应该可以解决依赖项彼此不兼容的问题。


总之,问题是依赖项彼此不兼容。但是,有一个修复方法!

$ npm install --legacy-peer-deps

这里的问题是“sax”的peer dependencies

有一些方法可以避免检查对等依赖性。

方法一

npm install <package name> --force

npm install <package name> --legacy-peer-deps

但不推荐,

将 --legacy-peer-deps 添加到您的 npm 安装将绕过 peerDependency auto-installation,但这可能会由于潜在的破坏性更改而导致冲突

尝试通过使用

获取对等依赖项详细信息来手动解决冲突
npm info name-of-module peerDependencies

更多信息请参考: