Replace npm package with native implementation

In my .json package, I have a dependency of "protractor": "2.1.0" . This package in turn depends on "jasminewd2": "0.0.5" .

There is a behavior in the jasminewd2 package that I want to change. I downloaded its sources and made the changes that I need. The yarn documentation talks about the possibility of using a local source for packages:

yarn add file:/path/to/local/folder installs a package that resides on the local file system. This is useful for testing your other packages that have been published to the registry.

When I execute this command

  • "jasminewd2": "file:\\CustomNodeModules\\jasminewd2" added to my package.json package.
  • And this is to my yarn.lock file:

     "file:\\CustomNodeModules\\jasminewd2", jasminewd2@0.0.5 : name jasminewd2 version "0.0.5" resolved "https://registry.yarnpkg.com/jasminewd2 /-/jasminewd2-0.0.5.tgz#528609a124dfc688c1e3f434a638e047066cd63e" 

As a result, node_modules/jasminewd2 contains the original version from the npm repository. How can I get yarn to install my version instead?

+7
javascript npm yarnpkg
source share
2 answers

I believe your solution does not work, because jasminewd2 is a transitive dependency ( protractor ), not a direct one. Therefore, when you add it directly, the transition is not affected.

You can get around this using two approaches:

  • If your change is temporary (intended for development or troubleshooting), you should yarn link , as described in the documentation .
  • Otherwise, you can unblock the protractor and jasminewd2 and refer to them in the corresponding package.json s. package.json syntax for this is "protractor": "<githubUser>/<githubRepo>" .

From my experience, the second approach has a caveat in the form of an npm cache: your git repo HEAD pulled only when this dependency is first set . After that, it is cached and constantly reinstalled - even when your HEAD repo has changed.

That's why I usually refer to a hash commit as part of a dependency like: "dependency": "user/repo.git#aef38fb2adc73304ae1ea87b0d607ad7fadc4d0g" . I have not tried this trick with yarn , but I assume that it behaves in exactly the same way (by design via a lock file).

+5
source share

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 } # func user repo branch dest override_pkg johndoe myrepo:branch mynpmpackage 

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 .

+1
source share

All Articles