nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.56k stars 2.36k forks source link

`nx migrate --run-migrations` installs deps with `legacy-peer-deps=true` (even for projects without this legacy config) #26675

Open nsbarsukov opened 4 months ago

nsbarsukov commented 4 months ago

Current Behavior

Use project without legacy-peer-deps=true.

Run the following command

nx migrate --run-migrations

it logs:

 NX   Running 'npm install' to make sure necessary packages are installed

removed 297 packages, and audited 2714 packages in 4s

and then throws error:

Cannot find module 'ts-morph'

(actually, this dependency node_modules/ts-morph existed before running nx migrate --run-migrations).


Use the following command instead

npm_config_legacy_peer_deps=false nx migrate --run-migrations

Everything is okay, no error.

Expected Behavior

Default command

nx migrate --run-migrations

works without error for default project configuration (without legacy-peer-deps=true).

GitHub Repo

https://github.com/taiga-family/maskito/pull/1337

Steps to Reproduce

  1. Clone this repository https://github.com/taiga-family/maskito

  2. Switch to branch nx-legacy-peer-deps-demo

  3. Run the following commands

    nvm use 18
    npm ci
    nx migrate --run-migrations

    It will throw error

  4. Then revert changes inside package-lock.json and run again npm ci.

  5. Run the following command

    npm_config_legacy_peer_deps=false nx migrate --run-migrations

Everything is okay, no error.

Read more in-depth description in the body of this PR:

Nx Report

NX   Report complete - copy this into the issue template

Node   : 18.19.1
OS     : darwin-arm64
npm    : 10.2.4

nx (global)    : 19.0.3
nx             : 19.3.1
@nx/js         : 19.3.1
@nx/jest       : 19.3.1
@nx/linter     : 19.3.1
@nx/eslint     : 19.3.1
@nx/workspace  : 19.3.1
@nx/angular    : 19.3.1
@nx/cypress    : 19.3.1
@nx/devkit     : 19.3.1
@nx/react      : 19.3.1
@nx/rollup     : 19.3.1
@nrwl/tao      : 19.3.1
@nx/web        : 19.3.1
@nx/webpack    : 19.3.1
typescript     : 4.9.5

Failure Logs

No response

Package Manager Version

No response

Operating System

Additional Information

Probably, the problem relates to the following lines from nx-library:

if (detectPackageManager() === 'npm') {
    process.env.npm_config_legacy_peer_deps ??= 'true';
}
denny99 commented 3 months ago

I was always wondering why the npm i command, when used in the NX Plugins, removes some packages.

But perhaps this is quite important?

E.g. when upgrading packages from one version to another. Without the legacy peer deps the install might fail, as some packages will get their new version only after the upgrade (e.g. Angular CLI Version gets only upgraded during the actual migration execution)

Maybe an npm i -f might be a solution?

cainlevy commented 1 month ago

Workaround is to run npm install yourself:

npm install
NX_MIGRATE_SKIP_INSTALL=true npx nx _migrate --run-migrations --if-exists

I plan to make this a script in my root package.json.

therockstorm commented 1 month ago

Another workaround is:

npm_config_legacy_peer_deps=false npx nx migrate --run-migrations --if-exists