nrwl / nx

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

CLI binaries from root/node_modules/.bin not found when running package.json scripts of subprojects with Nx #28483

Open mklueh opened 1 week ago

mklueh commented 1 week ago

Current Behavior

Hello, I've migrated my workspace from yarn to pnpm with the hope to get around this issue: https://github.com/wxt-dev/wxt/issues/1058 with success so far.

However, now I'm facing a new problem which I haven't had before, and I'm not sure if it is Nx related, pnpm related or has something to do with the new Node installation (used Vola over NVM to install Node and pnpm).

I'm trying to run a build script from the package.json

  "scripts": {
    "print-env-info": "bash -c 'echo ${SHELL:-/bin/bash}; echo ${LANG:-en_US.UTF-8}; printenv'",
    "build": "nuxt generate" //pnpm nuxt generate also fails
}

and I'm getting this output error

$ pnpm nx run my-app:build

> my-project@1.0.0 nx C:\Users\MyPc\Workspace\my-project
> nx "run" "my-app:build"

> nx run my-app:build

> my-app@0.29.1-WS-899-alpha.0 build C:\Users\MyPc\Workspace\my-project\apps\my-app
> nuxt generate
Der Befehl "nuxt" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
 ELIFECYCLE  Command failed with exit code 1.

For whatever reason, it is english mixed with german.

When I navigate to apps/my-app in the terminal and run nuxt build it works perfectly.

So it seems Nx has different environment variables than the Git bash I'm using in IntelliJ

To verify that, I've compared the output of running pnpm nx run my-app:print-env-info with running echo ${SHELL:-/bin/bash}; echo ${LANG:-en_US.UTF-8}; printenv manually in the terminal, and indeed, while the Nx command only points to ..../my-project/apps/my-app/node_modules/.bin the manual command from the terminal points to the root node_modules/.bin!

Another observation is that running the script in package.json with IntelliJ's RunConfiguration also fails with the same error.

This issue also appears to happen in GitHub Actions.

  1. Why do / can the environment variables differ?
  2. How can I tell Nx to use the root-level node_modules/.bin for whatever command it executes?

Expected Behavior

To behave identically to the local shell and find the binaries in the submodules

GitHub Repo

No response

Steps to Reproduce

As I have no idea what may influence the environment variables Nx is using (or even the shell) I'd like to get some information first to narrow down the scope

Nx Report

Node : 22.9.0 OS : win32-x64 Native Target : x86_64-windows pnpm : 9.12.1

nx : 20.0.1 @nx/js : 20.0.1 @nx/jest : 20.0.1 @nx/eslint : 20.0.1 @nx/workspace : 20.0.1 @nx/devkit : 20.0.1 @nrwl/devkit : 19.6.0 @nx/esbuild : 20.0.1 @nx/eslint-plugin : 20.0.1 @nx/express : 20.0.1 @nx/node : 20.0.1 @nx/nuxt : 20.0.1 @nx/playwright : 20.0.1 @nx/vite : 20.0.1 @nx/vue : 20.0.1 @nx/web : 20.0.1 @nx/webpack : 20.0.1 typescript : 5.6.3

Registered Plugins: @nx/vite/plugin @nx/eslint/plugin @nx/playwright/plugin

Community plugins: @jnxplus/nx-gradle : 1.14.0 @theunderscorer/nx-semantic-release : 2.12.0

The following packages should match the installed version of nx

Failure Logs

No response

Package Manager Version

pnpm latest

Operating System

Additional Information

No response

jaysoo commented 14 hours ago

Hi @mklueh ,

You may need to check your terminal settings in IntelliJ to ensure that it is using git bash, and not something else like powershell. That could be the reason that your environment variables are different. https://www.jetbrains.com/help/idea/settings-tools-terminal.html

For your build script, you could use npx nuxt instead of just nuxt as a workaround.

mklueh commented 12 hours ago

Hey @jaysoo ,

I'm using Git Bash in IntelliJ already and this shell is aware of the .bin directory.

However it seems nx:run-scripts is then launching CMD instead of the shell it is launched from, which was not aware of the directory. Maybe that's the default shell used by Node.

As a workaround I've added the absolute path to my project's node_modules/.bin to my Windows Environment variables, as bad as that sounds. It works for me, because I only have one project I'm currently using on my machine.