libyear
libyear copied to clipboard
Crashes with Cannot read property 'replace' of undefined
🐛 Bug Report
When trying to run npx libyear
, on one project it crashes and I just get
Cannot read property 'replace' of undefined
🎛 Configuration
I do not have a libyear configuration.
🤔 Expected Behavior
It should run as normal.
😯 Current Behavior
Crashes with Cannot read property 'replace' of undefined
. No stack trace is shown.
💁 Possible Solution
N/A
🔦 Context
💻 Code Sample
🌍 Your Environment
It works fine for most of my projects, just not one of them.
Software | Version(s) |
---|---|
libyear | [email protected] |
Node | 12.20.2 |
Package Manager | npm 7.24.0 |
Operating System | macOs 10.15.7 |
I had the same problem when running with npx, but it worked in the same package if I installed libyear first.
I noticed this error when the actually installed packages is node_modules/
don't correspond to the package.json and lockfile, for example due to switching branches. npm ci
made it work for me so far.
Maybe that could be a lead to tracking down this issue and providing a better error message?
I experienced this problem as well, and found a fix and a workaround. In my case this is caused by a workspaces
entry in my package.json
.
This section seems to be causing problems, when the resolved dependency starts with file://
(i.e. a local reference):
https://github.com/jdanil/libyear/blob/6a2bc4620ea8238d40ab262fb8ffda6fc2572152/src/dependencies.ts#L82-L86
I was able to patch this by modifying the function in dependencies.js of node_modules to include a filter before mapping:
return new Map(Object.entries({
...json.dependencies,
...json.devDependencies,
})
.filter(([dependency, data]) => !data.resolved.startsWith('file:'))
.map(([dependency, data]) => [
Workaround
Delete the workspaces entry from your package.json, delete node_modules, and npm install again. I know you may want to also scan the workspaces, but I hope this gets you a step further.
You can run npm pkg delete workspaces
to automate this in a script if you don't want to permanently remove the workspaces.
The same issue happens when you have optional peer dependencies in your package.json
which end up not being installed, i.e. something like
"peerDependencies": {
"some-package": "^1.2.3"
},
"peerDependenciesMeta": {
"some-package": {
"optional": true
}
}
Such a construct can be used to express "I don't require some-package, but if you use it, it should be ^1.2.3
", i.e. "I'm incompatible with versions before 1.2.2". If such a package is not used, the output of npm ls --depth=0 --json --silent
contains
"dependencies": {
...
"some-package": {}
}
This causes the Cannot read property 'replace' of undefined
crash.