Search code examples
dockernpm-linkvscode-devcontainer

npm link dev packages when using docker dev containers


Use npm link for authoring multiple packages simultaneously in docker dev containers

PkgA is a dependency of PkgB, I'm making changes to both. Goal is to be able to link PkgA in PkgB without publishing each small update and re-installing. npm|yarn link solve this, but I'm developing in docker containers.

https://github.com/npm/npm/issues/14325


Solution

  • 1. Create a directory on the host machine to serve as the global repo

    (I like to make a docker dir and put all of my volumes in it)

    mkdir -p ~/docker/volumes/yalc
    

    2. Mount the volume in both (or more) dev containers

    https://code.visualstudio.com/docs/remote/containers-advanced

    devcontainer.json
    
    ...
    "mounts": ["source=/Users/evan/docker/volumes/yalc,target=/yalc,type=bind,consistency=cached"],
    ...
    

    and rebuild the container

    3. Install yalc and publish the package (In dependency repo container)

    https://www.npmjs.com/package/yalc

    npm i yalc -g
    yalc publish --store-folder /yalc
    

    --store-folder tells yalc to publish the repo to our volume

    4. Link to the package in consuming repo

    consider adding yalc to .gitignore first:

    .yalc
    yalc.lock
    

    Run the link command

    npm i yalc -g
    yalc link PkgA --store-folder /yalc
    

    Where PkgA is the name of the package as defined in it's package.json