title: How to cache node_modules for unchanged package.json during yarn / npm install during docker build?
body: <p>I have a Dockerfile for a node application looking like this:</p>
  
  <pre><code>FROM node:8.3
  ENV TERM=xterm-color NPM_CONFIG_LOGLEVEL=warn PATH="$PATH:/usr/src/app/node_modules/.bin/"
  WORKDIR /usr/src/app
  ADD . /usr/src/app
  RUN yarn install --frozen-lockfile --ignore-platform --ignore-engines --quiet &amp;&amp; tsc &amp;&amp; webpack
  CMD ["node", "/usr/src/app/server"]
  </code></pre>
  
  <p>Yet this means that on <em>every</em> change to the sourcetree, the layer with <code>RUN yarn install ...</code> will be invalidated and run again.</p>
  
  <p>As yarn install by itself takes about 80s, the built is unnecessarily slowed down even if only a <code>Readme.md</code> changed.</p>
  
  <p>I want to use only run <code>yarn install</code> once either the package.json or <code>yarn.lock</code> changes.</p>
  
  <p>This question would also apply to npm and its packlage</p>