On the project I am joining this is the architecture for the node_packages
:
|- Django project |-- app1 |-- app2 |-- node_modules |--- foundation-sites |--- grunt |-- static |--- css |--- images |--- js |--urls.py |--settings.py |--package.json
I personally think node_packages
should be in the static under the js
folder as well as the package.json
like so:
|- Django project |-- app1 |-- app2 |-- static |--- css |--- images |--- js |---- node_modules |----- foundation-sites |----- grunt |---- packages.json |--urls.py |--settings.py
is there a difference? which is best practice? why?
Setting Up JavascriptYou can also now use npm packages in your django app. For example, if you wanted to use jQuery simply install it. And import it in your script. js file.
You can't merge them. You can send messages from Django to Node. Js through some queue system like Reddis.
How many node packages are there? There are 350,000 node packages. NPM registry contains the most populated package registry. This is a fact that it is considered to be the largest package registry in the world.
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.
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.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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With