Omar Omeiri
Omar Omeiri

Reputation: 1825

Automatic npm install --legacy-peer-deps for a single dependency

Assume I have a package.json like this:

{
  "name": "my-app",
  "version": "0.1.0",
  "dependencies": {
    "@aws-sdk/client-s3": "^3.21.0",
    "@testing-library/react": "^11.2.5",
    "axios": "^0.22.0",
    "credit-card-type": "^8.3.0",
    "csstype": "^3.0.8",
    "dayjs": "^1.10.4",
    "lodash": "^4.17.20",
    "mathjax-full": "^3.2.0",
    "mathjax-react": "^1.0.6",
    "react": "^17.0.2",
  },
  "proxy": "http://localhost:5000",
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "jest",
    "eject": "react-scripts eject",
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

The problem is that the dependency mathjax-react & mathjax-full need react@"^15.0.0 || ^16.0.0". I have tested with npm i --force and npm i --legacy-peer-deps and It all seems to work fine with my react version which is [email protected].

I wouldn't like to run npm i --force and npm i --legacy-peer-deps every time i need to install the dependencies, So I was looking for a way to automatically do that only for the mathjax-react & mathjax-full whenever I run npm i. I tried looking in the .npmrc docs and this reference, but could not find a way to do that. What might be a solution for this? Is there a native npm solution for this? Or do i have to write a script that reads my package.json and runs npm install for every dependency alone?

My reasoning is that if i need to install some other dependencies that have conflicts, I'd never be warned about it.

Upvotes: 10

Views: 7318

Answers (1)

Malvineous
Malvineous

Reputation: 27300

Legacy peer dependencies aren't per-package, they apply to the project as a whole. It's just that you might need it for your whole project to keep only one or two dependencies happy. You'll find if you install a package with --legacy-peer-deps and then install a different package without that flag, npm will complain about the first package again. So once you need --legacy-peer-deps, you need to specify it always no matter what package you install.

According to the docs, you can set it permanently with:

npm config set legacy-peer-deps=true --location=project

This just adds legacy-peer-deps=true to the end of the .npmrc in your project root.

Upvotes: 18

Related Questions