nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
106.42k stars 29k forks source link

Add `--root-directory` optional flag for `node --run` #53831

Open manoraj opened 1 month ago

manoraj commented 1 month ago

What is the problem this feature will solve?

Currently, when running node --run, it searches recursively for the package.json file up to the root directory. Additionally, it appends the /node_modules/.bin paths up to the root directory to the PATH environment variable.

It would be nice to restrict recursively searching till a certain directory if it is provided as an option node --run test --root-directory=<monorepo-root>.. This is particularly helpful in a monorepos with multiple/multi-level workspaces where we don't expect the bin paths to be added beyond the monorepo directory and also do not look for package.json beyond that point. In some cases, it will help to avoid any discrepancies between local and CI

What is the feature you are proposing to solve the problem?

Add an optional flag --root-directory or a similar one, which will be used as a root directory instead of cwd.root_path() here.

What alternatives have you considered?

No response

anonrig commented 1 month ago

Can you share an example of how the current implementation does not work for your monorepo?

manoraj commented 1 month ago

Hi @anonrig

We have monorepo architecture where we have package.json at the root of the repo and a separate package.json in the respective services.

Sharing the directory structure below --

monorepo-root/package.json monorepo-root/service-1/package.json monorepo-root/service-2/package.json ...

Let's say we have a dependency package in monorepo-root/package.json but not in monorepo-root/service-1/package.json, running node --run from services will also add a bin folder from monorepo-root to the PATH which is not desired and also can lead to inconsistent behaviour between local and CI (when CI does dependency install only at the service level).

RedYetiDev commented 1 month ago

That sounds more like a bug to me, but IIRC the run command is supposed to use the closest package.json (and its directory) for everything, right?

manoraj commented 1 month ago

I am not sure if it's a bug. It's similar to what Node does in general regarding how it searches for a package.

That sounds more like a bug to me, but IIRC the run command is supposed to use the closest package.json (and its directory) for everything, right? It does use the closest package.json but adds the bin folder up to the root directory.