nx: nx migrate fails with Yarn berry

Current Behavior

Running nx migrate latest with yarn@3.5.0 fails.

Expected Behavior

nx migrate latest should run without any errors

GitHub Repo

https://github.com/nrwl/nx-examples

Steps to Reproduce

Run the following commands (assuming volta is installed)

  1. volta install yarn@3
  2. yarn
  3. nx migrate latest

Nx Report

>  NX   Report complete - copy this into the issue template

   Node : 16.17.0
   OS   : darwin arm64
   yarn : 3.5.0

   nx                      : 15.9.0-rc.2
   @nrwl/js                : 15.9.0-rc.2
   @nrwl/jest              : 15.9.0-rc.2
   @nrwl/linter            : 15.9.0-rc.2
   @nrwl/workspace         : 15.9.0-rc.2
   @nrwl/angular           : 15.9.0-rc.2
   @nrwl/cli               : 15.9.0-rc.2
   @nrwl/cypress           : 15.9.0-rc.2
   @nrwl/devkit            : 15.9.0-rc.2
   @nrwl/eslint-plugin-nx  : 15.9.0-rc.2
   @nrwl/react             : 15.9.0-rc.2
   @nrwl/tao               : 15.9.0-rc.2
   @nrwl/web               : 15.9.0-rc.2
   @nrwl/webpack           : 15.9.0-rc.2
   @nrwl/nx-cloud          : 15.3.3
   typescript              : 4.9.5
   ---------------------------------------
   Community plugins:
   @ngrx/component-store : 15.3.0
   @ngrx/effects         : 15.3.0
   @ngrx/entity          : 15.3.0
   @ngrx/router-store    : 15.3.0
   @ngrx/store           : 15.3.0
   @ngrx/store-devtools  : 15.3.0

Failure Logs

/bin/sh: /var/folders/tf/tjgc6tp54151mmr3jxmgcdr40000gq/T/tmp-32796-hV5BmtocRoSJ/node_modules/.bin/nx: No such file or directory
Command failed: /var/folders/tf/tjgc6tp54151mmr3jxmgcdr40000gq/T/tmp-32796-hV5BmtocRoSJ/node_modules/.bin/nx _migrate latest

Additional Information

After checking the temporary folders listed in the errors, there is no node_modules folder.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 15
  • Comments: 18 (5 by maintainers)

Most upvoted comments

Was able to get this to work by directly running ./node_modules/.bin/nx _migrate latest twice from the repo root. First pass seemed to update the packages, second pass created the migrations file.

TL; DR; Workaround

For everyone affected by this problem, please follow the workaround proposed by @arvidboivie to use environment variable override. This is needed only for the nx migrate ... commands.

YARN_NODE_LINKER="node-modules" yarn nx migrate latest
yarn # no env var needed
YARN_NODE_LINKER="node-modules" yarn nx migrate --run-migrations

More details

When running nx migrate ... commands to upgrade from version A to version B, we need to run the migration command using the new version B. In order to do this, we generate a temp folder where we install version B and then run the nx migrate pointing to your repo. This works as expected for all cases apart from one. Sadly, that one affects you.

If you use Yarn Berry with nodeLinker set to node-modules everything works fine inside your repo, but that temp folder doesn’t know about the nodeLinker settings (which is a bug we need to fix) so it uses the default, PnP installation.

Based on your repo, nx is still expecting to find binary in the node_modules\nx\.bin, but that doesn’t exist for PnP, and you end up with that ugly error.

Solution

Now that we know the problem, we’ll see the fix is implemented as soon as possible. Sorry for the delay.

Thank you all for your contribution to clarification of this issue.

Still fails applying migrations with these workarounds. Yarn 3.4.1, Nx 16.7.2, Node 18.13.0.

Edit: Running YARN_NODE_LINKER="node-modules" NX_MIGRATE_SKIP_INSTALL=1 yarn nx migrate --run-migrations solved my issue as the error comes when Nx runs yarn install before running migrations

This fails with PNP enabled, as there is no node_modules/.bin/nx.

It is very annoying, but the “workaround” of falling back on node_modules via the YARN_NODE_LINKER="node-modules" environment variable seems to work…

YARN_NODE_LINKER="node-modules" yarn
YARN_NODE_LINKER="node-modules" yarn nx migrate latest
YARN_NODE_LINKER="node-modules" yarn nx migrate --run-migrations
yarn

(the last yarn just deletes all the node_modules that are not necessary because PNP)

There’s another workaround that’s setting YARN_NODE_LINKER="node-modules" for your command, i.e. YARN_NODE_LINKER="node-modules" yarn nx migrate latest.

I think the issue is that yarn defaults to pnp mode when used by nx, and it doesn’t read your local .yarnrc file to check settings.