cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] npm workspaces issue when using different versions of the same package

Open Maxim-Mazurok opened this issue 3 years ago • 5 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

This issue exists in the latest npm version

  • [X] I am using the latest npm

Current Behavior

"testing jest-26"
28.1.0 <<< unexpected

"testing jest-28"
28.1.0

Expected Behavior

"testing jest-26"
26.6.3 <<< expected

"testing jest-28"
28.1.0

Steps To Reproduce

  1. git clone https://github.com/Maxim-Mazurok/npm-workspaces-repro
  2. Use node v18.3.0 and [email protected]: nvm i 18.3.0 or nvs add 18.3.0 && nvs use 18.3.0
  3. npm ci
  4. npm test

Environment

  • npm: 8.12.1
  • Node.js: 18.3.0
  • OS Name: Windows 11
  • System Model Name: X570 GAMING X
  • npm config:
; "user" config from C:\Users\maxim\.npmrc
   
registry = "https://registry.npmjs.org/" 

; node bin location = C:\Users\maxim\AppData\Local\nvs\node\18.3.0\x64\node.exe
; node version = v18.3.0
; npm local prefix = C:\Users\maxim\npm-test
; npm version = 8.12.1
; cwd = C:\Users\maxim\npm-test
; HOME = C:\Users\maxim
; Run `npm config ls -l` to show all defaults.

Maxim-Mazurok avatar Jun 04 '22 06:06 Maxim-Mazurok

Workaround:

Use jest programmatically: node -e "console.log(require('jest').getVersion())"

See https://github.com/Maxim-Mazurok/npm-workspaces-repro/commit/eed5ec8691b229512d102999a06af015c7b38036

Maxim-Mazurok avatar Jun 04 '22 07:06 Maxim-Mazurok

I was able to reproduce this on Windows, but not on other platforms.

fritzy avatar Jun 29 '22 21:06 fritzy

The %PATH% is generated correctly for each script. The %CD% is correct for each script. \node_modules\jest\package.json is 26.x.x \jest-26\node_modules\jest\package.json is 28.x.x

But when executing \node_modules\.bin\jest.cmd from any other path other than the project root, it'll resolve jest@28 rather than jest@26

fritzy avatar Jun 29 '22 22:06 fritzy

Running npm ci on the root folder adds node _modules for the root folder and for jest-28 folder but does not do the same for the jest-26 folder.

When I run npm ci in the jest-26 folder to generate the node_modules and redo the npm test on the root folder, I get the expected value for jest-26 but an error for jest-28 as shown below.

> test
> npm test --workspaces


> test
> echo "testing jest-26" && jest --version

"testing jest-26"
26.6.3

> test
> echo "testing jest-28" && jest --version

"testing jest-28"
node:internal/modules/cjs/loader:1051
 throw err;
 ^

Error: Cannot find module 'jest-cli/bin/jest'
Require stack:
- C:\Users\user\Documents\npm-workspace-repro\jest-28\node_modules\jest\bin\jest.js
   at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
   at Module._load (node:internal/modules/cjs/loader:901:27)
   at Module.require (node:internal/modules/cjs/loader:1115:19)
   at require (node:internal/modules/helpers:130:18)
   at Object.<anonymous> (C:\Users\user\Documents\npm-workspace-repro\jest-28\node_modules\jest\bin\jest.js:12:3)
   at require (node:internal/modules/helpers:130:18)
   at Object.<anonymous> (C:\Users\user\Documents\npm-workspace-repro\jest-28\node_modules\jest\bin\jest.js:12:3)
   at Module._compile (node:internal/modules/cjs/loader:1241:14)
   at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
   at Module.load (node:internal/modules/cjs/loader:1091:32)
   at Module._load (node:internal/modules/cjs/loader:938:12)
   at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) {
 code: 'MODULE_NOT_FOUND',
 requireStack: [
   'C:\\Users\\user\\Documents\\npm-workspace-repro\\jest-28\\node_modules\\jest\\bin\\jest.js'
 ]
}

Node.js v20.7.0
npm ERR! Lifecycle script `test` failed with error:
npm ERR! Error: command failed
npm ERR!   in workspace: jest-28
npm ERR!   at location: C:\Users\user\Documents\npm-workspace-repro\jest-28

These findings bring about the questions.

If npm ci on a root folder is supposed to install packages on sub folders, then why did it do it for just one sub folder and not the other?

siemhesda avatar Feb 08 '24 19:02 siemhesda

After thorough testing, we identified the issue does not exist in the new version of node and npm. By switching to node 18.18.2 or higher versions, you can achieve the expected results. img

cc@wraithgar

siemhesda avatar Feb 21 '24 19:02 siemhesda

This looks like it's fixed as of 10.8.1. Please updated npm.

~/workarea/rep/npm-workspaces-repro $ npm test

> test
> npm test --workspaces


> test
> echo "testing jest-26" && jest --version

testing jest-26
26.6.3

> test
> echo "testing jest-28" && jest --version

testing jest-28
28.1.0

milaninfy avatar Jul 12 '24 20:07 milaninfy