Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy Node.js application with deep node_modules structure on Windows?

I've run into a curious issue - apparently some Node.js module have so deep folder hierarchies that Windows copy command (or PowerShell's Copy-Item which is what we're actually using) hits the infamous "path too long" error when path is over 250 chars long.

For example, this is a folder hierarchy that a single Node module can create:

node_modules\nodemailer\node_modules\simplesmtp\node_modules\ xoauth2\node_modules\request\node_modules\form-data\node_modules\ combined-stream\node_modules\delayed-stream\... 

It seems insane but is a reality with Node modules.

We need to use copy-paste during deployment (we're not using a "clever" target platform like Heroku where Git deployment would be an option) and this is a serious limitation on Windows.

Isn't there a npm command or something that would compact the node_modules folder or maybe include only what's actually necessary at runtime? (Node modules usually contain test folders etc. which we don't need to deploy.) Any other ideas how to work around it? Not using Windows is unfortunately not an option :)

like image 461
Borek Bernard Avatar asked Nov 10 '12 01:11

Borek Bernard


People also ask

How do I deploy a node JS application as a Windows service?

var Service = require('node-windows'). Service; // Create a new service object var svc = new Service({ name:'Hello World', description: 'The nodejs.org example web server. ', script: 'C:\\path\\to\\helloworld. js' }); // Listen for the "install" event, which indicates the // process is available as a service.

Do I need to push node_modules in production?

Not committing node_modules implies you need to list all your modules in the package. json (and package-lock. json ) as a mandatory step. This is great because you might not have the diligence to do so, and some of the npm operations might break if you don't.


1 Answers

just to add to this... another thing that helped me was listing out all installed modules with npm ls.

which will give you a tree of modules and versions... from there it's pretty easy to identify which ones are duplicates... npm dedupe didn't do anything for me. I'm not sure if that's a bug or what (Node v 10.16)

So once you identify a duplicate module install it to the root node_module directory by using npm install [email protected] --save-dev. The version is important.

after that, I wiped out my node_modules directory and did a fresh npm install.

Short version

  1. npm ls to get a list of all installed modules.
  2. look through those modules and identify duplicate modules (version is important)
  3. npm install module@version --save-dev to install those modules in the root node_modules directory and update package.json.
  4. rmdir node_modules to delete the node_modules directory.
  5. npm install to pull down a fresh copy of your dependencies.

Once I did that, everything was much cleaner.

I also recommend commenting your package.json file to show which ones were brought down to flatten the node_modules tree.

like image 155
Ben Lesh Avatar answered Oct 07 '22 21:10

Ben Lesh