cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] npm link <package>: Cannot read properties of null (reading 'matches')

Open Ploppz opened this issue 3 years ago • 27 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

Running npm link react in my library gives me the following output:

$ npm link react
npm ERR! Cannot read properties of null (reading 'matches')

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/ploppz/.npm/_logs/2022-02-03T11_48_09_016Z-debug-0.log

And no link happens. The log:

Expected Behavior

No error, and symbolic link created in node_modules with name react

Steps To Reproduce

I have npm installed from official Arch repo.

I have an app called ui. I went into its node_modules/react after npm i and ran sudo npm link. As such, a symbolic link was created called /usr/lib/node_modules/react which points to this directory:

$ ls -l /usr/lib/node_modules
lrwxrwxrwx  1 root   root     49 Jan 31 14:30  react -> ../../../home/ploppz/ui/node_modules/react/
(rest redacted)

I have a shared library called components in which I need to link to this react installation, so I run npm link react in there and get the above problem. npm link react worked fine yesterday with the same setup, and it still work perfectly fine in a second application I'm developing with the same components library... It's just in the components library itself it won't work.

Note that also the components library itself is linked with sudo npm link

Environment

  • npm: 8.3.2
  • Node.js: 17.3.0
  • OS Name: Arch Linux
  • System Model Name: ?
  • npm config:
; node bin location = /usr/bin/node
; cwd = /home/ploppz/components
; HOME = /home/ploppz
; Run `npm config ls -l` to show all defaults.

Ploppz avatar Feb 03 '22 12:02 Ploppz

When in the components lib I manually do:

rm -rf node_modules/react
ln -s /usr/lib/node_modules/react node_modules/react

Then in another application (not the aforementioned ui application) in which I have ran npm link react:

[email protected] /home/ploppz/admin-ui
├─┬ [email protected] -> ./../components
│ ├── [email protected] -> ./../ui/node_modules/react
│ └─┬ [email protected]
│   ├─┬ [email protected]
│   │ └── [email protected] deduped -> ./../ui/node_modules/react
│   └── [email protected] deduped -> ./../ui/node_modules/react
└── [email protected] -> ./../ui/node_modules/react

Output redacted to retain only the relevant entries. Now I really wonder: why is there no deduped behind the react dep of components? They point to the exact same location! This results in the application throwing an error:

Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

Which I know is exactly because of duplicate installations of react - it always works well when there is deduped on all but one react in npm ls react.

The reason I share this is because: today strange things started happening that broke any understanding I thought I had about how npm link and deduping of deps should work, and makes me think that something somewhere is terribly wrong.

Ploppz avatar Feb 03 '22 12:02 Ploppz

It works fine in any other directory:

/tmp  $ mkdir a ; cd a
/tmp/a  $ npm link react

added 1 package, and audited 3 packages in 432ms

found 0 vulnerabilities
/tmp/a  $ ls node_modules
react@

Ploppz avatar Feb 03 '22 13:02 Ploppz

I figured out exactly what is needed to reproduce it, and as such how I could solve it - that is, it still seems like an issue with npm. Here is my package.json

{
  "name": "components",
  "version": "1.1.2",
  "description": "",
  "main": "lib/index.js",
  "keywords": [],
  "author": "",
  "license": "ISC",

  "devDependencies": {
    "@babel/cli": "^7.16.8",
    "@babel/core": "^7.16.12",
    "@babel/preset-env": "^7.16.11",
    "@babel/preset-react": "^7.16.7"
  },
  "peerDependencies": {
    "react": "*",
    "styled-components": "*"
  },
  "scripts": {
    "watch": "babel --watch src --out-dir lib --copy-files",
    "build": "babel src --out-dir lib --copy-files",
    "prepare": "npm run build",
    "test": "echo \"Error: no test specified\" && exit 1",
    "link": "npm link react"
  }
}

If I remove "styled-components": "*" from "peerDependencies" it works fine. It's beyond me why this should be a problem. So if you create an empty directory with this package.json and you have a link to react in your global packages, I think you might be able to reproduce.

Ploppz avatar Feb 03 '22 13:02 Ploppz

You need to link every peer dep also - which means react also needs to be linked.

ljharb avatar Feb 03 '22 15:02 ljharb

Thanks for your input - if so, I suggest this issue stays open until npm returns a suitable error message on this error. Curiously, I could not find anything when searching for Cannot read properties of null (reading 'matches')

Ploppz avatar Feb 03 '22 15:02 Ploppz

That error is indeed an npm bug. The react warning you see is because of the general concept that when linking, every peer dep must also be linked, so they’re not duplicated.

ljharb avatar Feb 03 '22 15:02 ljharb

Node: v17.4.0 Npm : v8.3.1 OS: Windows 10 21H2 19044.1415

TypeError: Cannot read properties of null (reading 'matches')
    at Link.matches (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\node.js:1090:41)
    at Link.canDedupe (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\node.js:1044:15)
    at PlaceDep.pruneDedupable (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:465:14)
    at PlaceDep.placeInTree (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:326:14)
    at PlaceDep.place (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:214:10)
    at new PlaceDep (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:71:10)
    at PlaceDep.placeInTree (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:362:26)
    at PlaceDep.place (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:214:10)
    at new PlaceDep (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\place-dep.js:71:10)
    at C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:949:31

kenneththerodgers avatar Feb 05 '22 04:02 kenneththerodgers

Looks like we need a more sensible error here.

fritzy avatar Feb 24 '22 21:02 fritzy

Any solution? I'm experiencing this error across all dev machines. It's a showstopper.

I'm running all the latest: npm: '8.6.0', node: '17.9.0'

aggiehorns avatar Apr 13 '22 01:04 aggiehorns

I've got the same problem on 18.1 / 8.8:

5994 verbose stack TypeError: Cannot read properties of null (reading 'matches')
5994 verbose stack     at Link.matches (/Users/johndoe/.asdf/installs/nodejs/18.1.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/node.js:1095:41)
5994 verbose stack     at Link.canDedupe (/Users/johndoe/.asdf/installs/nodejs/18.1.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/node.js:1049:15)
5994 verbose stack     at PlaceDep.pruneDedupable (/Users/johndoe/.asdf/installs/nodejs/18.1.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/place-dep.js:468:14)
5994 verbose stack     at PlaceDep.placeInTree (/Users/johndoe/.asdf/installs/nodejs/18.1.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/place-dep.js:329:14)
5994 verbose stack     at PlaceDep.place (/Users/johndoe/.asdf/installs/nodejs/18.1.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/place-dep.js:216:10)
5994 verbose stack     at new PlaceDep (/Users/johndoe/.asdf/installs/nodejs/18.1.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/place-dep.js:73:10)
5994 verbose stack     at /Users/johndoe/.asdf/installs/nodejs/18.1.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/build-ideal-tree.js:988:31
5994 verbose stack     at Array.map (<anonymous>)
5994 verbose stack     at [buildDepStep] (/Users/johndoe/.asdf/installs/nodejs/18.1.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/build-ideal-tree.js:988:8)
5994 verbose stack     at async Arborist.buildIdealTree (/Users/johndoe/.asdf/installs/nodejs/18.1.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/build-ideal-tree.js:218:7)
5994 verbose stack     at async Promise.all (index 1)
5994 verbose stack     at async Arborist.reify (/Users/johndoe/.asdf/installs/nodejs/18.1.0/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:153:5)
5994 verbose stack     at async Install.exec (/Users/johndoe/.asdf/installs/nodejs/18.1.0/lib/node_modules/npm/lib/commands/install.js:156:5)
5994 verbose stack     at async module.exports (/Users/johndoe/.asdf/installs/nodejs/18.1.0/lib/node_modules/npm/lib/cli.js:78:5)
5995 verbose cwd /Users/johndoe/code/my-project
5996 verbose Darwin 21.4.0
5997 verbose node v18.1.0
5998 verbose npm  v8.8.0
5999 error Cannot read properties of null (reading 'matches')
6000 verbose exit 1

When running: npm i --save-dev sb && npm sb init --type react --builder webpack5 -f

benvds avatar May 16 '22 18:05 benvds

I was having the same exact issue after I did pnpm install and then npm install @serverless-stack/cli --save-exact.

101 verbose Darwin 21.5.0
102 verbose node v18.4.0
103 verbose npm  v8.12.1

I deleted my node_modules folder and tried npm install and that worked

JoshuaKemmerer avatar Jul 05 '22 19:07 JoshuaKemmerer

I have the same problem as @JoshuaKemmerer with pnpm, hoping it will be fixed soon

VanishMax avatar Jul 21 '22 14:07 VanishMax

I was so unlucky (yesterday) that all the problems I encountered were officially unsolved......

GrinZero avatar Aug 23 '22 17:08 GrinZero

For anyone interested. I had this issue as well, until I realised I needed to use pnpm instead of npm.

So in my case I would use pnpm to install my package because that's the tool I ran the install command with.

Kordonme avatar Aug 29 '22 20:08 Kordonme

For anyone interested. I had this issue as well, until I realised I needed to use pnpm instead of npm.

So in my case I would use pnpm to install my package because that's the tool I ran the install command with.

Yes, pnpm can run.

GrinZero avatar Aug 30 '22 03:08 GrinZero

https://github.com/nestjs/nest-cli/issues/1720 revelant

MarvinXu avatar Nov 22 '22 07:11 MarvinXu

  • I had a project built around pnpm
  • Accidentally ran npx which is from npm instead of pnpm dlx
  • it created a package-lock.json
  • running pnpm dlx after that brought this error
  • deleting package-lock.json cleared the issue

atahrijouti avatar Dec 20 '22 16:12 atahrijouti

I have a vite project with PNPM and React

  • ran pnpm dlx storybook init
  • deleted pnpm-lock.yaml to be sure

Got this error:

 storybook init - the simplest way to add a Storybook to your project. 

 • Detecting project type. ✓
    Detected vite project, setting builder to @storybook/builder-vite
 • Adding Storybook support to your "React" app
npm ERR! Cannot read properties of null (reading 'matches')

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/mikaelboutin/.npm/_logs/2023-01-19T20_44_45_824Z-debug-0.log
An error occurred while installing dependencies.

 ERROR  Command failed with exit code 1: /Users/mikaelboutin/Library/pnpm/store/v3/tmp/dlx-24501/node_modules/.bin/storybook init

pnpm: Command failed with exit code 1: /Users/mikaelboutin/Library/pnpm/store/v3/tmp/dlx-24501/node_modules/.bin/storybook init
    at makeError (/usr/local/Cellar/pnpm/7.18.2/libexec/dist/pnpm.cjs:22852:17)
    at handlePromise (/usr/local/Cellar/pnpm/7.18.2/libexec/dist/pnpm.cjs:23423:33)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.handler [as dlx] (/usr/local/Cellar/pnpm/7.18.2/libexec/dist/pnpm.cjs:202479:7)
    at async /usr/local/Cellar/pnpm/7.18.2/libexec/dist/pnpm.cjs:209921:21
    at async main (/usr/local/Cellar/pnpm/7.18.2/libexec/dist/pnpm.cjs:209892:34)
    at async runPnpm (/usr/local/Cellar/pnpm/7.18.2/libexec/dist/pnpm.cjs:210123:5)
    at async /usr/local/Cellar/pnpm/7.18.2/libexec/dist/pnpm.cjs:210115:7

Can someone help me finding what's missing for it to run correctly please?

DWboutin avatar Jan 19 '23 20:01 DWboutin

solved by pnpm rebuild for me

kokizzu avatar Feb 01 '23 20:02 kokizzu

I encounter this issue when I was using PNPM but need npm i --package-lock-only to produce a package-lock.json.

samson-sham avatar Apr 06 '23 19:04 samson-sham

I encounter this issue when I was using PNPM but need npm i --package-lock-only to produce a package-lock.json.

I also ran into the same issue, it seems that --package-lock-only argument didn't ignore checking node_modules, you can rename node_modules folder:

mv node_modules node_modules_foobar
npm install --package-lock-only
mv node_modules_foobar node_modules

luy19 avatar Apr 21 '23 03:04 luy19

I have this issue when running...

[p]npm version patch // from lib folder inside packages folder, OR [p]npm version patch --workspace lib // from root folder

...since I converted my application to pnpm. In the root package.json I've included local dependencies that seems to break these commands

"dependencies": {
    "lib": "workspace:*"
}

I have no peerDependencies as mentioned above. Is this an pnpm issue or is it a npm issue?

xania avatar May 13 '23 15:05 xania

imc it worked after deleting node_modules and .cache https://stackoverflow.com/questions/74650198/npm-err-cannot-read-properties-of-null-reading-matches

daveleee avatar Jun 27 '23 22:06 daveleee

For me was just deletion of node_modules. If you are using workspaces and have several node_modules / package.json, for each module, then delete them all and try npm i again!

razlani avatar Jul 08 '23 04:07 razlani

actual error message is misleading

npm ERR! Cannot read properties of null (reading 'matches')

expected error message would be more helpful

npm ERR! node_modules/ was installed by pnpm, please use 'pnpm install'

milahu avatar Aug 11 '23 08:08 milahu

@Ploppz also, in case you haven't figured this part out yet, npm should never be installed separately, it comes with node and should be upgraded with itself; and i suggest not using apt to install node.

ljharb avatar Aug 11 '23 17:08 ljharb