How to develop node library referenced via local directory from another project?

Per this answer, when you reference a local dependency in package.json the local package will be copied to node_modules. This is not ideal when I'm developing a package and just referencing it from another project as I want to verify the library works correctly within another project. It seems every time I make a change to the library, I have to go back to my consuming project, delete the node_modules/my-library folder and rerun npm install each time for it to copy the library back. If I don't delete the folder first it doesn't seem to copy the latest version over.

If I develop directly inside node_modules/my-library it's not ideal because that folder isn't version controller, unlike the local folder referenced in package.json.

Another option would be to create an example project within the my-library repo, but I'd prefer to go that route as a last resort.

1 answer

  • answered 2018-02-13 02:47 Bulkan

    You can use npm link to develop your library and have another project use the local version like so.

    For example;

    In the my-library directory run npm link.

    Then in the project you want to use my-libray run npm link my-library.

    This will also work with yarn link.