Best practice for nodejs deployment - Directly moving node_modules to server or run npm install command
Running npm install
in production server cannot be done in certain scenario (lack of compiling tools, restricted internet access, etc...) and also if you have to deploy the same project on multiple machines, can be a waste of cpu, memory and bandwidth.
You should run npm install --production
on a machine with the same libraries and node version of the production server, compress node_modules and deploy on production server. You should also keep the package-lock.json
file to pinpoint versions.
This approach allows you also to build/test your code using development packages and then pruning the node_modules before the actual deploy.
- Moving node_modules folder is overkilled.
- Running
npm install
might break the version dependencies. - The best approach is
npm ci
. It uses the package_lock file and installs the required dependencies without modify the versions. npm ci meant for continuous integration projects. LINK