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

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


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 dupemodule@1.2.3 --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.


I don't think there's any great solution given your constraints, but here are some things that may help.

  • Try using npm dedupe to optimize your directory hierarchy which may shorten some paths
  • Use npm install --production to install without the development tools
  • Take some of those deeply nested dependencies (just enough to avoid the problem, I suggest) and move them to the top-level node_modules directory. Just keep track of them so you know which are your true dependencies and which are workarounds for this problem.
  • OR move some of those deep dependencies to the highest node_modules directory under your_project/node_modules/pkg_with_deep_deps that will allow them to have short enough paths but still work. So this would be your_project/node_modules/pkg_with_deep_deps/node_modules.
    • I think require should be able to find those properly at run time. You'll just need to clearly document what you have manually changed, why you have done it, and keep your own true dependencies accurately represented in package.json

Here is a github issue discussion that elaborates on this problem in detail.


npm v3(released recently) solves this issue by flattening out the dependencies.. Check the release notes here in https://github.com/npm/npm/releases/tag/v3.0.0 under flat flat section.

And the last comment on this issue https://github.com/npm/npm/issues/3697