在翻看metamask-extension源码时候看到package.json中有个字段"packageManager": “yarn@4.9.1”,但在npm中查看yarn,最新版还是1.22.22,奇怪,那么实际项目怎么安装这个v4的yarn呢。仔细查下发现,其中是使用了corepack来做包管理器和版本管理。
8月 12, 2025
While browsing the metamask-extension source code, I saw a field in package.json: “packageManager”: “yarn@4.9.1”, but when checking yarn on npm, the latest version is still 1.22.22. Strange, so how does the actual project install this v4 yarn? After careful investigation, I found that it uses corepack for package manager and version management.
8月 12, 2025
EventEmitter Initial Version class EventEmitter{ constructor(){ this.events={} } on(type,listener){ if(!this.events[type]){ this.events[type]=[] } this.events[type].push(listener) } emit(type,...args){ this.events[type].forEach(listener=>{ listener.call(this,...args) }) } off(type,listener){ if(this.events[type]){ const index=this.events[type].indexOf(listener) if(index!==-1){ this.events[type].splice(index,1) } } } once(type,listener){ const onceListener=(...args)=>{ listener.call(this,...args) this.off(type,onceListener) } this.on(type,onceListener) } } Testing const eventEmitter=new EventEmitter() const listener=(args)=>{ console.log(args); } eventEmitter.once('test',listener) eventEmitter.off('test',listener) eventEmitter.emit('test',{a:1}) When executing the above code, you’ll find that the test event was triggered once, but it should not execute because it was turned off. The solution is as follows.
3月 18, 2025
EventEmitter初版 class EventEmitter{ constructor(){ this.events={} } on(type,listener){ if(!this.events[type]){ this.events[type]=[] } this.events[type].push(listener) } emit(type,...args){ this.events[type].forEach(listener=>{ listener.call(this,...args) }) } off(type,listener){ if(this.events[type]){ const index=this.events[type].indexOf(listener) if(index!==-1){ this.events[type].splice(index,1) } } } once(type,listener){ const onceListener=(...args)=>{ listener.call(this,...args) this.off(type,onceListener) } this.on(type,onceListener) } } 测试 const eventEmitter=new EventEmitter() const listener=(args)=>{ console.log(args); } eventEmitter.once('test',listener) eventEmitter.off('test',listener) eventEmitter.emit('test',{a:1}) 执行上述代码,会发现test事件被触发了一次,预期应该是不执行的,因为off了。解决办法如下。
3月 18, 2025
使用npm开发中会发现,比如将node_modules文件夹删除,然后重新安装依赖,会发现安装速度非常快,这是因为npm有缓存机制。
2月 13, 2025
When developing with npm, you may notice that if you delete the node_modules folder and then reinstall dependencies, the installation is very fast. This is because npm has a caching mechanism.
2月 13, 2025
最近看一些项目发现依赖包很多,随便拿了几个搜索下,发现并没有用,因此可以删除了。但这样的包,一个个查询检索确认删除还是太麻烦。 想着怎么能高效的检测并删除不用的依赖包,基于这个需求,我写了一个小工具。
1月 26, 2025
package.json中除了dependency和devDependency之外,还有一个peerDependency。这里通过解决一个实际报错来了解下它。
1月 2, 2025
Besides dependency and devDependency in package.json, there’s also peerDependency. Let’s resolve a real-world error to understand how it works. Error Message npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: a@0.0.1-beta npm ERR! Found: ssh2@1.15.0 npm ERR! node_modules/ssh2 npm ERR! ssh2@"1.15.0" from the root project npm ERR! ssh2@"^1.12.0" from ssh2-sftp-client@9.1.0 npm ERR! node_modules/ssh2-sftp-client npm ERR! ssh2-sftp-client@"^9.1.0" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer ssh2@"1.16.0" from a@0.0.1-beta npm ERR! packages/a npm ERR! a@0.0.1-beta npm ERR! node_modules/a npm ERR! workspace packages/a from the root project npm ERR! npm ERR! Conflicting peer dependency: ssh2@1.16.0 npm ERR! node_modules/ssh2 npm ERR! peer ssh2@"1.16.0" from a@0.0.1-beta npm ERR! packages/a npm ERR! a@0.0.1-beta npm ERR! node_modules/a npm ERR! workspace packages/a from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! /Users/alanhe/.npm/_logs/2025-01-02T02_53_13_141Z-eresolve-report.txt npm ERR! A complete log of this run can be found in: npm ERR! /Users/alanhe/.npm/_logs/2025-01-02T02_53_13_141Z-debug-0.log Error Analysis The project’s direct dependency pins ssh2 at version 1.15.0, while package a lists ssh2@1.16.0 in its peerDependency field.
1月 2, 2025
项目中经常会使用很多第三方的NPM包,比如Mousetrap,很多时候会遇到bug,但是官方的bug还没修复或者还没合并发包,那么这个时候怎么办呢。这里总结下方法。
10月 24, 2024