npm / cli

the package manager for JavaScript
https://docs.npmjs.com/cli/
Other
8.34k stars 3.07k forks source link

[BUG] Error on npm install with workspaces: "Cannot set properties of null (setting 'dev')" #3901

Open alexpusch opened 2 years ago

alexpusch commented 2 years ago

Is there an existing issue for this?

Current Behavior

In an npm workspaces project we have a usecase of nested package - in the directory of package "a" we have a directory containing another package "a-inner". A different package "b" depends on "a-inner"

trying to npm install only package "b": npm install --workspace @npmwsbug/b results in an error

log lines from npm log:

58 verbose stack TypeError: Cannot set properties of null (setting 'dev')
58 verbose stack     at calcDepFlagsStep (/home/alex/.npm-packages/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/calc-dep-flags.js:34:21)
58 verbose stack     at visit (/home/alex/.npm-packages/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/calc-dep-flags.js:12:20)
58 verbose stack     at visitNode (/home/alex/.npm-packages/lib/node_modules/npm/node_modules/treeverse/lib/depth-descent.js:57:25)
58 verbose stack     at next (/home/alex/.npm-packages/lib/node_modules/npm/node_modules/treeverse/lib/depth-descent.js:44:19)
58 verbose stack     at depth (/home/alex/.npm-packages/lib/node_modules/npm/node_modules/treeverse/lib/depth-descent.js:82:10)
58 verbose stack     at depth (/home/alex/.npm-packages/lib/node_modules/npm/node_modules/treeverse/lib/depth.js:27:12)
58 verbose stack     at calcDepFlags (/home/alex/.npm-packages/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/calc-dep-flags.js:10:15)
58 verbose stack     at Arborist.[copyIdealToActual] (/home/alex/.npm-packages/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:1400:7)
58 verbose stack     at Arborist.reify (/home/alex/.npm-packages/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:153:35)
58 verbose stack     at async Install.install (/home/alex/.npm-packages/lib/node_modules/npm/lib/install.js:170:5)

Expected Behavior

workspace should be installed successfully

Steps To Reproduce

clone following example repo - https://github.com/alexpusch/npm-ws-bug-example run npm install --workspace @npmwsbug/b

error will appear: TypeError: Cannot set properties of null (setting 'dev')

Environment

nperez0111 commented 2 years ago

I was able to get around this by doing a npm ci

ruyadorno commented 2 years ago

Thanks for reporting it @alexpusch! I can reproduce based on your minimal example:

$ cat package.json
{
  "name": "nested-pkg-in-ws",
  "version": "1.0.0",
  "workspaces": [
    "a",
    "b"
  ]
}

$ cat a/package.json
{
  "name": "a",
  "version": "1.0.0"
}

$ cat b/package.json
{
  "name": "b",
  "version": "1.0.0"
}

$ cat a/c/package.json
{
  "name": "c",
  "version": "1.0.0"
}

$ npm i -w b file:a/c --verbose
npm verb cli [
npm verb cli   'node',
npm verb cli   '$HOME/Documents/workspace/cli/main',
npm verb cli   'i',
npm verb cli   '-w',
npm verb cli   'b',
npm verb cli   'file:a/c',
npm verb cli   '--verbose'
npm verb cli ]
npm info using npm@8.3.2
npm info using node@v16.13.2
...
npm verb stack TypeError: Cannot set properties of null (setting 'dev')
npm verb stack     at calcDepFlagsStep ($HOME/Documents/workspace/cli/main/workspaces/arborist/lib/calc-dep-flags.js:34:21)
npm verb stack     at visit ($HOME/Documents/workspace/cli/main/workspaces/arborist/lib/calc-dep-flags.js:12:20)
npm verb stack     at visitNode ($HOME/Documents/workspace/cli/main/node_modules/treeverse/lib/depth-descent.js:57:25)
npm verb stack     at next ($HOME/Documents/workspace/cli/main/node_modules/treeverse/lib/depth-descent.js:44:19)
npm verb stack     at depth ($HOME/Documents/workspace/cli/main/node_modules/treeverse/lib/depth-descent.js:82:10)
npm verb stack     at depth ($HOME/Documents/workspace/cli/main/node_modules/treeverse/lib/depth.js:27:12)
npm verb stack     at calcDepFlags ($HOME/Documents/workspace/cli/main/workspaces/arborist/lib/calc-dep-flags.js:10:15)
npm verb stack     at Arborist.[copyIdealToActual] ($HOME/Documents/workspace/cli/main/workspaces/arborist/lib/arborist/reify.js:1472:7)
npm verb stack     at Arborist.reify ($HOME/Documents/workspace/cli/main/workspaces/arborist/lib/arborist/reify.js:155:35)
npm verb stack     at async Install.exec ($HOME/Documents/workspace/cli/main/lib/commands/install.js:157:5)
npm verb cwd <path>/nested-pkg-in-ws
npm verb Darwin 19.6.0
npm verb argv "node" "$HOME/Documents/workspace/cli/main" "i" "-w" "b" "file:a/c" "--verbose"
npm verb node v16.13.2
npm verb npm  v8.3.2
npm ERR! Cannot set properties of null (setting 'dev')
npm verb exit 1
npm timing npm Completed in 238ms
npm verb unfinished npm timer reify 1643153208927
npm verb unfinished npm timer reify:audit 1643153208956
npm verb unfinished npm timer auditReport:getReport 1643153208956
npm verb code 1

npm ERR! A complete log of this run can be found in:
npm ERR!     $HOME/.npm/_logs/2022-01-25T23_26_48_750Z-debug-0.log

Interesting thing to note is that despite the error it seems that the installation concluded successfully in my case:

$ tree node_modules
node_modules
├── a -> ../a
├── b -> ../b
└── c -> ../b/a/c

$ git diff b/package.json
diff --git a/b/package.json b/b/package.json
index c2d84cc..b3bab6a 100644
--- a/b/package.json
+++ b/b/package.json
@@ -1,4 +1,7 @@
 {
   "name": "b",
-  "version": "1.0.0"
+  "version": "1.0.0",
+  "dependencies": {
+    "c": "file:a/c"
+  }
 }

And any npm install after that will just work as intended:

$ npm i

changed 1 package, and audited 7 packages in 467ms

found 0 vulnerabilities
justinfagnani commented 2 years ago

I hit this problem just now when running npm ci accidentally inside a workspace folder - I didn't know my cwd wasn't in the top-level of the monorepo.

"Cannot set properties of null" was a very bad message and I took a while of updating npm and node before I figured out the simple mistake of being in the wrong directory. I think that npm should check to see if it's inside a workspace and give a more helpful error.

nephix commented 1 year ago

I'm having the same issue running npm ci -w workspace in the repository root

Seems to have been introduced by the recently updated node:16-alpine Docker image

wjureczka commented 1 year ago

Same issue with node lts 16 and 18, combining different npm versions from 7 to 9.

comp615 commented 10 months ago

Still seeing this; hard to tell from navigating all the "cannot set properties of null" issues to know if they are related or not. Seems to be failing when we run install-related commands inside any workspace package folder. Tried to run ci at the root to clean stuff out as suggested, also tried to go back to our last npm/node version but not really having much luck. Happy to debug with someone locally if that's useful. We use a monorepo with workspaces, and I know this worked at one point.

It almost feels like node_modules and lockfile pollution where a dependency is there and just sticks around and can't be removed.

IJustDev commented 8 months ago

I got the same issue when deleting the node_modules folder from my app with turborepo.

rharkor commented 6 months ago

same here with turborepo

brnhffmnn commented 1 month ago

Are there any known workarounds?