'Resolve peer dependency of aliased package breaks npm install

I'm trying to test my npm package against different versions of React. So I set up aliases for older version in package.json:

{
  "name": "gatsby-plugin-i18n-l10n",
  [..]
  "peerDependencies": {
    "gatsby": "^4.x",
    "gatsby-source-filesystem": "^4.x",
    "react-helmet": "^6.1.x",
    "react-intl": "^5.20.x"
  },
  "peerDependenciesMeta": {
    "gatsby-source-filesystem": {
      "optional": true
    }
  },
  "devDependencies": {
    [..]
    "react": "18.0.0",
    "react-17": "npm:[email protected]",
    "react-dom": "18.0.0",
    "react-dom-17": "npm:[email protected]",
    [..]
    "react-test-renderer": "18.0.0",
    "react-test-renderer-17": "npm:[email protected]",
    [..]
  },
  "dependencies": {
    "limax": "^3.0.0",
    "path-browserify": "^1.0.1"
  }
}

While researching this I've found another question which is similar NPM: Link peer dependency to package alias, which works, but npm install still throws the following error:

npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/react
npm ERR!   dev react@"18.0.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"17.0.2" from [email protected]
npm ERR! node_modules/react-dom-17
npm ERR!   dev react-dom-17@"npm:[email protected]" 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.

How can I make npm install work again and help it to resolve the packages by itself?



Solution 1:[1]

*I rewrote my comment as I did not get the problem at first

It is possible, but you will need to manually edit the package-lock file of NPM.

In steps as follows:

  1. Leave only the legacy dependencies in the package.json file like so:
    {
      "name": "npmpeerstest",
      "version": "1.0.0",
      "description": "",
      "type": "module",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "@legacy/react": "npm:react@^17.0.2",
        "@legacy/react-dom": "npm:react-dom@^17.0.2"
      }
    }
  1. Run npm install, it should pass ok
  2. Edit package-lock.json from this:
"node_modules/@legacy/react-dom": {
      "name": "react-dom",
      "version": "17.0.2",
      "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-17.0.2.tgz",
      "integrity": "sha512-s4h96KtLDUQlsENhMn1ar8t2bEa+q/YAtj8pPPdIjPDGBDIVNsrD9aXNWqspUe6AzKCIG0C1HZZLqLV7qpOBGA==",
      "dev": true,
      "dependencies": {
        "loose-envify": "^1.1.0",
        "object-assign": "^4.1.1",
        "scheduler": "^0.20.2"
      },
      "peerDependencies": {
        "react": "17.0.2"
      }
    },

to:

"node_modules/@legacy/react-dom": {
      "name": "react-dom",
      "version": "17.0.2",
      "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-17.0.2.tgz",
      "integrity": "sha512-s4h96KtLDUQlsENhMn1ar8t2bEa+q/YAtj8pPPdIjPDGBDIVNsrD9aXNWqspUe6AzKCIG0C1HZZLqLV7qpOBGA==",
      "dev": true,
      "dependencies": {
        "loose-envify": "^1.1.0",
        "object-assign": "^4.1.1",
        "scheduler": "^0.20.2"
      },
      "peerDependencies": {
        "@legacy/react": "17.0.2"
      }
    },

Notice that we need to manually set the react dependency to use the @legacy/react instead of react

  1. Add new dependencies to package.json like so:
{
  "name": "npmpeerstest",
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@legacy/react": "npm:react@^17.0.2",
    "@legacy/react-dom": "npm:react-dom@^17.0.2",
    "react": "18.0.0",
    "react-dom": "18.0.0"
  }
}
  1. Run npm install, and all should pass

*do not forget to commit the package-lock.json with the modification or other installs will fail

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1