Ben
Ben

Reputation: 16524

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?

Upvotes: 2

Views: 2847

Answers (2)

Jasper Woudenberg
Jasper Woudenberg

Reputation: 1166

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.

Upvotes: 1

VonC
VonC

Reputation: 1323953

When you make a change in the submodule, that means you have to make a commit in the parent repo (in order to record the new gitlink, a special entry in the index which memorize the new SHA1 of your submodule)

Why not use that commit opportunity to execute automatically a script which will modify the package.json file with the right information?

That is called a 'clean' script from a content filter driver:

clean

(image shown in "Customizing Git - Git Attributes", from "Pro Git book")

The clean script is:

  • declared with git config filter.<filtername>.clean ./<filterscript>
  • used in a .gitattributes file.

Its function will be to fetch the version of the npm submodule and update the package.json file appropriately.

Upvotes: 0

Related Questions