An image is made of stacked layers, each coming from a Dockerfile instruction. Docker reuses unchanged layers from the cache. You should therefore place the instructions that rarely change (installing dependencies) before those that change often (source code).
Example
COPY package*.json ./ RUN npm ci COPY . .