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

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

Open
2 tasks done
Ploppz opened this issue Feb 3, 2022 · 27 comments
Open
2 tasks done
Labels
Bug thing that needs fixing Priority 2 secondary priority issue Release 8.x work is associated with a specific npm 8 release

Comments

@Ploppz
Copy link

Ploppz commented Feb 3, 2022

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • 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 Ploppz added Bug thing that needs fixing Needs Triage needs review for next steps Release 8.x work is associated with a specific npm 8 release labels Feb 3, 2022
@Ploppz
Copy link
Author

Ploppz commented Feb 3, 2022

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:

admin-ui@0.1.0 /home/ploppz/admin-ui
├─┬ components@1.1.2 -> ./../components
│ ├── react@17.0.2 -> ./../ui/node_modules/react
│ └─┬ styled-components@5.3.3
│   ├─┬ react-dom@17.0.2
│   │ └── react@17.0.2 deduped -> ./../ui/node_modules/react
│   └── react@17.0.2 deduped -> ./../ui/node_modules/react
└── react@17.0.2 -> ./../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
Copy link
Author

Ploppz commented Feb 3, 2022

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
Copy link
Author

Ploppz commented Feb 3, 2022

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.

@ljharb
Copy link
Contributor

ljharb commented Feb 3, 2022

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

@Ploppz
Copy link
Author

Ploppz commented Feb 3, 2022

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')

@ljharb
Copy link
Contributor

ljharb commented Feb 3, 2022

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.

@kenneththerodgers
Copy link

kenneththerodgers commented Feb 5, 2022

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

@fritzy
Copy link
Contributor

fritzy commented Feb 24, 2022

Looks like we need a more sensible error here.

@fritzy fritzy added Priority 2 secondary priority issue and removed Needs Triage needs review for next steps labels Feb 24, 2022
@aggiehorns
Copy link

aggiehorns commented Apr 13, 2022

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'

@benvds
Copy link

benvds commented May 16, 2022

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

@JoshuaKemmerer
Copy link

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

@VanishMax
Copy link

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

@GrinZero
Copy link

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

@Kordonme
Copy link

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.

@GrinZero
Copy link

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.

@MarvinXu
Copy link

nestjs/nest-cli#1720 revelant

@atahrijouti
Copy link

atahrijouti commented Dec 20, 2022

  • 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

@DWboutin
Copy link

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?

@kokizzu
Copy link

kokizzu commented Feb 1, 2023

solved by pnpm rebuild for me

@samson-sham
Copy link

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

@luy19
Copy link

luy19 commented Apr 21, 2023

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

@xania
Copy link

xania commented May 13, 2023

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?

@daveleee
Copy link

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

@razlani
Copy link

razlani commented Jul 8, 2023

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!

@milahu
Copy link

milahu commented Aug 11, 2023

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'

@ljharb
Copy link
Contributor

ljharb commented Aug 11, 2023

@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.

@YolandaMua
Copy link

I've got the same problem, and I solved.
step1. npm link
step2. npm prefix -g
then I got my link path
/Users/Yolanda/.nvm/versions/node/v16.20.2
step3. pnpm link /Users/Yolanda/.nvm/versions/node/v16.20.2/lib/node_modules/mp-kit

20240703-120439

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Priority 2 secondary priority issue Release 8.x work is associated with a specific npm 8 release
Projects
None yet
Development

No branches or pull requests