1

Makefile:

node_modules: package.json ## Run npm install
        @npm install

Why does make node_modules excecute the receipe npm install every time I run make node_modules?

I believe the recipe should not get executed if my file package.json is not more recent that the directory node_modules.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
smarber
  • 4,829
  • 7
  • 37
  • 78

1 Answers1

7

The link that @IsmailBadawi provided in comments is the key here.

Make determines if a target is out of date based on its last-modified time (mtime). However, a directory is only considered to be modified when one of its direct children are added/removed/renamed (at least on Unix-like systems).

This is insufficient in your case. Imagine the following sequence:

  1. Run make for the first time.

    • Rule is not skipped.
    • mtime(node_modules) > mtime(package.json).
  2. Run make again.

    • Rule is skipped.
  3. Change a dependency version in package.json.

    • mtime(node_modules) < mtime(package.json).
  4. Run make again.

    • Rule is not skipped.
    • Dependency is updated in node_modules.
    • But mtime(node_modules) is unaffected.
  5. Run make again.

    • Rule is not skipped.
  6. ... and so on forever ...

You can probably fix this by adding touch -m node_modules to your recipe.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680