Overriding a submodule with postinstall and git
The "npm way" requires a specific commit (or version / tag) to install the package from the git repository, more information on configuring npm . Usually I create a custom branch and click on it, so I need the ability to clone a custom branch to test a specific function or just leave only the wizard.
To override the package, I use putinstall hook for npm.
I add a script to postinstall in package.json :
"scripts": { "postinstall": "./postinstall.sh", "start": "node index.js" },
Then I use a bash script ( postinstall.sh ) to remove packages and clone them from github:
#!/bin/sh function override_pkg { USER=$1 REPO=$2 DEST=$3 rm -rf node_modules/$DEST echo "Overriding $DEST..." if [ -d custom_modules/$DEST ]; then cd custom_modules/$DEST git pull cd ../../ else case $REPO in *:*) REPOBR=(${REPO//:/ }) git clone -b ${REPOBR[1]} https://github.com/$USER/${REPOBR[0]}.git custom_modules/$DEST ;; *) git clone https://github.com/$USER/$REPO.git custom_modules/$DEST ;; esac fi npm install custom_modules/$DEST }
The script clones the package in the custom_modules folder, then it uses npm to install the local package on node_modules . The name of the branch after : is optional.
In the case of OP, there should be something like this:
override_pkg johndoe jasminewd:jasminewd2 jasminewd2
Then yarn:
yarn
If I make local changes to the local git repository in custom_modules\mypackage , I call yarn again to override pacakge.
If I changed my machine and I already have a local version of my custom package, calling yarn pull it out of the repository and override pacakge.
PS. npm install also works, but yarn works a little faster.
Multiple Testing Environments
This is a different idea, but sometimes I used it to test various configurations. Maybe people may find this useful, or if anyone knows a tool or something like that, tell me.
Locally, I create two different folders for node_modules as follows:
First source modules:
yarn --modules-folder=original-modules
Flags:
--modules-folder <path> instead of installing the modules in the node_modules folder relative to cwd, print them here.
Then for custom, I can just copy the original-modules for the clone:
cp -r original-modules custom-modules
Or I can use yarn to add additional custom modules:
yarn add <modulename> --no-lockfile --modules-folder=custom-modules
Flags:
--no-lockfile do not read or create a lock file
When I'm happy with the custom-modules folder, I can switch between environments such as NODE_PATH :
For normal env:
NODE_PATH=original-modules npm start
Custom envrionment:
NODE_PATH=custom-modules npm start
It is important that the node_modules folder node_modules not exist or the override will not work, the local node_modules folder has a higher priority than NODE_PATH .