what is the best way to deal with local dependencies on submodules in node?

In our environment we have a node app with a dependency on a submodule which is also a node module. We ran into issues using npm link so we tried using local dependencies (i.e. setting the value of the dependency in package.json to file:./path/to/dep). The problem with this is that when you make a change in the submodule you have to then bump the version & update it in the parent. Is there a better way of dealing with this type of dependency so that I can just make changes in my submodule and have it just propagate to the parent?


Solution 1:

If you want changes you make in your submodule to be reflected immediately in your main module, the only way I know to achieve that is to create a symbolic link from your main module's node_modules/ directory to your submodule's directory. I really recommend finding out why npm link doesn't work for you, because it's the nicest way to acieve this. However, if you want to, you can create the link manually too.

For instance, if your submodule's package name is 'wonderful' and your file structure looks like this:

main-module/
    sub-module/

Then you can create a symbolic link main-module/node_modules/wonderful pointing to main-module/sub-module by running the following command from the root of the main module:

ln -s ../sub-module ./node_modules/wonderful

And then whatever change you make in the submodule will be immediately used in the main module.

Two notes on this:

  • Make sure main-module/node_modules/wonderful doesn't exist as an npm install-ed directory before creating the link, or it won't work.
  • When you npm install again, it will overwrite your symbolic link, so put the above command in a shell script if you want to execute it often.