merge two node_modules folders having separate package.json files

  docker, dockerfile, node-modules, node.js, npm-install

I am trying to install multiple package.json files in the same node_module folder. The reason behind separate package.json is I am using docker and I have some private modules which change frequently. Due to this, the docker reinstalls all modules whenever I build the image. It is time-consuming and also a waste of internet usage.

To overcome this issue, I have created separate private-package.json for my private modules.

COPY package.json package.json
RUN npm i
COPY private-package.json package.json
RUN npm i
CMD ["npm", "start" ]

But whenever I install this private-package.json, it replaces all old modules installed by package.json and if I see there are only those modules available in the node_modules folder which are installed using my private-package.json.

To overcome this, I used rsync as per the below approach,

COPY package.json package.json
RUN npm i && mv node_modules another_folder/
COPY private-package.json package.json
RUN npm i && rsync another_folder/node_modules node_modules
CMD ["npm", "start" ]

This solves my problem but is it the correct approach? OR is there any way form that will install in the same folder in node_modules? Can you please help me?

Thanks

Source: Docker Questions

LEAVE A COMMENT