2

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?

Ben
  • 16,124
  • 22
  • 77
  • 122
  • Why not just remove the submodule part and use either `npm link` or, since the submodule is also a node module, just add it as a regular dependency from `npmjs.com`? – Robert Rossmann Mar 17 '15 at 20:21

2 Answers2

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.
fregante
  • 29,050
  • 14
  • 119
  • 159
Jasper Woudenberg
  • 1,156
  • 9
  • 15
0

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The problem is more about node and less about the fact that it's a git submodule. And while your answer is clever and thoughtful, we can't commit until we test things out. having to do a commit with each change would mean we'd have a bunch of unnecessary commits. Thank you for your answer though – Ben Mar 14 '15 at 14:05
  • @Ben you can commit, and reset if it doesn't work out. You can push to an intermediate git repo for that (http://stackoverflow.com/a/3209767/6309: guarded commit) – VonC Mar 14 '15 at 14:08
  • thanks but yeah that's still not what I want to do. I should be able to just save changes and test them locally w/out having to do that. – Ben Mar 16 '15 at 16:20
  • @Ben Ok, I will delete the answer in a few days. – VonC Mar 16 '15 at 16:21
  • Sorry @VonC didn't want to belittle your answer, as I said, it's actually quite clever. it's just that from a dev workflow perspective it's basically the same problem we have today which is having to run scripts etc to keep things in sync... – Ben Mar 16 '15 at 16:25
  • @Ben Except the script is run automatically for you ;) – VonC Mar 16 '15 at 16:27