Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect PATH used in lerna exec #2850

Open
tex0l opened this issue Mar 18, 2021 · 3 comments
Open

Incorrect PATH used in lerna exec #2850

tex0l opened this issue Mar 18, 2021 · 3 comments

Comments

@tex0l
Copy link

tex0l commented Mar 18, 2021

The PATH used in lerna exec is not the same as the parent shell, which can result in a different npm version used.

I use npm@7.6.3 with node@14.16.0. I installed the node@14 package with Homebrew which initially comes with npm@6.14.11 and updated it with npm i -g npm --force. lerna doesn't use npm@7.6.3 but npm@6.14.11 which results in inconsistencies in lockfileVersions of the packages which should be in version 2, but are rolled back to version 1 since npm@6 is used instead of npm@7.

Expected Behavior

I'd expect the npm binary used in lerna exec to be the same as in the parent shell. This can be tested with which npm which should give the same result in the parent shell and in lerna exec -- which npm.

Current Behavior

In my environment, it does the following:

✗ which npm
/usr/local/bin/npm

and

✗ lerna exec -- which npm    
lerna notice cli v3.22.1
lerna info Executing command in 5 packages: "which npm"
/usr/local/Cellar/node@14/14.16.0_1/bin/npm
/usr/local/Cellar/node@14/14.16.0_1/bin/npm
/usr/local/Cellar/node@14/14.16.0_1/bin/npm
/usr/local/Cellar/node@14/14.16.0_1/bin/npm
/usr/local/Cellar/node@14/14.16.0_1/bin/npm
lerna success exec Executed command in 5 packages: "which npm"

Possible Solution

Lerna should use the same $PATH as the parent shell when creating the child processes.

Steps to Reproduce (for bugs)

I'm not entirely sure because I don't understand how the lerna PATH is set, but it happens in environments where multiple versions of npm are installed and the parent shell should have this npm binary in the path.

Context

I'm just trying to use lerna on my mac.

Your Environment


✗ lerna info
lerna notice cli v3.22.1

 Environment info:

  System:
    OS: macOS 11.2.3
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
  Binaries:
    Node: 14.16.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.6.3 - /usr/local/bin/npm
  Utilities:
    Git: 2.30.0 - /usr/local/bin/git
  npmPackages:
    lerna: ^3.22.1 => 3.22.1 
@rarkins
Copy link

rarkins commented Mar 18, 2021

I have observed the same problem in Ubuntu, so it's not Mac or Homebrew-specific. We worked around the problem by using Node v15 when calling lerna, which is not ideal.

@maverick1872
Copy link

Any further developments on this? This has been a pain point for me as well and I haven't yet determined a means of sourcing the necessary shell profile as a workaround (in my case my .zshrc) from within the child process that Lerna spawns.

@elementalvoid
Copy link

Experiencing this here too. We are still on Node v14 which, as stated, comes with NPM v6. We globally install NPM v7 like the other reporters.

Doing a little digging I found a workaround that doesn't seem horrible -- but first, let's examine the details...


Here's npm via my PATH:

❯ npm --version
7.24.2

❯ which npm
/Users/matt.klich/.asdf/shims/npm

asdf is in the game too; it shims around globally-installed npm modules. So npm in my shell is the globally-installed version.

Here's what lerna uses:

❯ lerna exec -- npm --version
info cli using local version of lerna
lerna notice cli v3.22.1
lerna info versioning independent
lerna info Executing command in 3 packages: "npm --version"
6.14.17
6.14.17
6.14.17
lerna success exec Executed command in 3 packages: "npm --version"

Of course, if we check with which npm we see that it's using the NodeJS default bundled npm instead of the globally-installed npm:

❯ lerna exec -- which npm
info cli using local version of lerna
lerna notice cli v3.22.1
lerna info versioning independent
lerna info Executing command in 3 packages: "which npm"
/Users/matt.klich/.asdf/installs/nodejs/14.20.0/bin/npm
/Users/matt.klich/.asdf/installs/nodejs/14.20.0/bin/npm
/Users/matt.klich/.asdf/installs/nodejs/14.20.0/bin/npm
lerna success exec Executed command in 3 packages: "which npm"

And we can verify the version of that:

❯ /Users/matt.klich/.asdf/installs/nodejs/14.20.0/bin/npm --version
6.14.17

My workaround is simple:

❯ npm i --save-dev npm@7

With that complete, lerna now uses NPM v7 like I want it to:

❯ lerna exec -- npm --version
info cli using local version of lerna
lerna notice cli v3.22.1
lerna info versioning independent
lerna info Executing command in 3 packages: "npm --version"
7.24.2
7.24.2
7.24.2
lerna success exec Executed command in 3 packages: "npm --version"

Works as a local fix and it'll help the CI scenario too. With the fix in place, our root package.json now has both npm and lerna as devDependencies. So, in CI, when we run:

npm ci
npm run bootstrap
npm test

It no longer matters whether lerna is executed from a global install (i.e. on my laptop) or from the local node_modules (i.e. in CI): either way, we're getting the desired npm version!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants