How to use your own fork from a node module library
Recently I came to a specific case where an SDK from a company was a bit outdated, missing an extra parameter available on their API that was needed in our project.
So I decided to fork the project, added the extra parameter to the SDK and created a PR. The thing is, meanwhile I wait for feedback and see if it gets merged I need to use that parameter!
So here are the options that allow our project to use the fork meanwhile we wait the maintainers to review and merge our PR
1. Change package.json dependency to point to your fork
Edit your dependencies, find the module you have forked and change it to point to your Github account.
From:
"dependencies" : {
"react": "^16.8.6",
"my-forked-plugin": "1.0.6",
"styled-components": "^4.3.1",
...
}
To
"dependencies" : {
"react": "^16.8.6",
"my-forked-plugin": ""https://github.com/blaiprat/my-forked-plugin.git",
"styled-components": "^4.3.1",
...
}
2. Run `npm i`
This will remove the old plugin, and download your version. Once the installation has finished there’s a chance that might work, but it’s quite possible that not. So we will need to add an extra step to your project.
3. Add a `postinstall` step
When publishing a module to npm, modules get built so they don’t require any additional step when we use them.
Module developers can use babel, typescript or any other library when developing it, and submit the final code built. This built code is generated and usually ignored on the original git repository.
So we will have to go inside the folder, npm install everything the module needs and build it. Once done it’s ready to be used.
Fortunately, we can use the default package.json
script called postinstall
This is how your package.json
scripts section should look like:
WARNING: be sure to check the build script name in the module package.json
"scripts": {
...
"postinstall": "cd ./node_modules/my-forked-project && npm install && npm run build"
}
There are three commands on this postinstall
step
1. cd ./node_modules/my-forked-project
will change the working directory to the fork
2. npm install
will use the forked module package.json to install the dependencies the module needs
3. Inspect the module package.json to find which is the script you need to call that will build the files so you can use them.
The process is not ideal, but will allow you and your teammates to be unblocked meanwhile the contributors of the module/library you’ve submitted the PR will have time to review it and merge it without pressure