외부 라이브러리를 입맛대로 바꾸기 위해 간단하게 patch-package 라이브러리를 사용해보았다.
설치
npm 또는 yarn을 이용해 설치한다. 난 yarn을 쓰기 때문에 yarn으로 설치해주었다.
yarn add patch-package postinstall-postinstall
postinstall 설치하는 이유
yarn은 postinstall hook을 yarn, yarn add 후에만 실행한다.
yarn remove후에도 확실하게 postinstall hook을 사용하기 위해 설치해준다.
GitHub - ds300/patch-package: Fix broken node modules instantly
- postinstall 이란?
패키지의 종속성 트리 변경 사항이 디스크에 기록되면(예: 종속성 또는 전이적 종속성이 추가, 제거 또는 변경된 후) 호출되는 yarn lifecycle script
postinstall is called after a package's dependency tree changes are written to the disk -- e.g. after a dependency or transitive dependency is added, removed, or changed.
It is guaranteed to be called in topological order (in other words, your dependencies' postinstalls will always run before yours).
For backwards compatibility, preinstall and install are called as part of postinstall.
설치를 해주었다면 package.json script에 다음과 같이 등록한다.
“scripts”: {
“postinstall”: “patch-package”
}
node_modules 안에 외부 라이브러리 수정이 끝나면
yarn postinstall 라이브러리이름
명령어로 변경사항을 저장할 수 있다.

‘dependencies’ of undefined
혹시 이 과정에서
TypeError: Cannot read property ‘dependencies’ of undefined
다음과 같은 에러가 발생한다면 package.lock.json, yarn.lock 파일을 삭제하고 다시 yarn후에 yarn postinstall 라이브러리이름 과정을 다시 해주면 된다.
나도 같은 에러가 발생했는데 package.lock.json에 patch-package 가 없어서 해당 에러가 발생했다.
(Make patch fails on package lock file dependencies check. · Issue #164 · ds300/patch-package · GitHub)

해당 과정까지 마치면 라이브러리의 변경사항이 변경된 파일로 올라와있는 것을 볼 수 있다.