django npm and node packages architecture

I understand your thinking of wanting to keep all the javascript related files in one place, but here are a couple of reasons you might want to keep the node_modules folder and the package.json file out of a Django app’s static directory.

  1. You’ll likely end up statically serving files that aren’t meant to be. If the node_modules folder exists in your production environment, running collectstatic will have to check that it’s in sync every time, which can be slow due to nodes nested dependency structure. And assuming you have a build step to bundle and transpile your JS, if those source files are within static, they too will be served as static files, for no reason.
  2. You might want to use node for more than just your JavaScript build process. I see you’re using Grunt, and you may want to use it to for more than your JavaScript needs, like minifying your css, or running a proxy server around your Django dev server that auto-reloads your browser when files change or the Django server restarts. With this in mind, it might make more sense to think of Node.js as a tool in your build process that could touch any part of your project, the bundling/transpiling of JavaScript being only one part of that.

Leave a Comment