The structure of Django, NPM, and Node packages

When considering the architecture of the project I will be working on, it's important to note the structure of the node_packages:

|- Django project
|-- app1
|-- app2
|-- node_modules
|--- foundation-sites
|--- grunt
|-- static
|--- css
|--- images
|--- js
|--urls.py
|--settings.py
|--package.json

In my opinion, relocating node_packages under the js folder in the static directory along with the package.json would be better, as shown below:

|- Django project
|-- app1
|-- app2
|-- static
|--- css
|--- images
|--- js
|---- node_modules
|----- foundation-sites
|----- grunt
|---- packages.json
|--urls.py
|--settings.py

Is there a significant difference between the two setups? Which approach is considered best practice and why?

Answer №1

While I understand the desire to centralize all JavaScript-related files in one location, there are compelling reasons to consider keeping the node_modules folder and the package.json file separate from a Django app's static directory.

  1. You risk serving unintended static files. Storing the node_modules folder within your production environment may result in lengthy synchronization checks during each collectstatic execution, particularly due to its complex dependency structure. Additionally, if your source JS files reside in the static directory, they will be needlessly served as static assets with no purpose.
  2. Node.js can serve broader purposes beyond just JS building. For instance, if you utilize Grunt for tasks beyond JavaScript, such as CSS minification or implementing a proxy server for your Django dev environment that supports auto-reloading, Node.js serves as a versatile tool within your project's build process. Consequently, it's advisable to view Node.js as an integral component capable of influencing multiple facets of your project rather than solely focusing on JS bundling/transpiling.

Answer №2

  • Place node_modules and package.json in the main directory of your project:

    • Convenient access to installing modules and running commands from the top level of your project
    • Dependencies easily visible at the top level, commonly found alongside pip requirements
    • Separation of external libraries/modules from your code
  • Add node_modules to your .gitignore file
  • Serve only generated files. Keep your source code outside the STATICFILES_DIRS

  • (Optional) If you prefer serving npm modules without using bower, consider using a tool like django-npm to specify what should be exposed

Check out some example projects:

https://github.com/mbrochh/django-reactjs-boilerplate

https://github.com/Seedstars/django-react-redux-base

Answer №3

Typically, it is recommended to keep the node_modules folder outside of the Django application structure. My preferred setup for a Django project usually looks like this:

- AppName
---- appname (Django Project)
---- appname-env (Python virtualenv)
---- bower_components
---- bower.json
---- gulpfile.js
---- node_modules
---- package.json
---- requirements.txt

Afterwards, I utilize gulp to transfer components from either node modules or bower components into the static/lib directory of my app.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Express server does not send any response body

When a request is made to my express application, it returns an empty {}. const express = require('express'); const bcrypt = require('bcrypt'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyPar ...

Node.js unexpectedly experiencing significant performance deterioration

In our current setup, we have an architecture consisting of 2 node processes. The first node process continuously monitors a private API for any changes and transmits them to the second node. The second node is responsible for processing this data, intera ...

Issue encountered: Trying to deploy firebase functions and hosting with vue-cli v3 and node.js leads to an error "No npm package found in functions source directory

My plan is to utilize Vue.js for the Frontend and Firebase Functions (Express.js) + Firestore for the Backend. Step 0: I initiated a new project on Google Firebase, then created a new Service Account with Owner's permissions to be used with Admin SDK ...

Steps for sharing a command line script on npm platform

Recently, I developed a node script that I'm looking to publish on npm. My goal is to make it easy for users to install globally and run with a simple command like scriptname args. Right now, I have to use node scriptname.js args to execute the script ...

How to troubleshoot Python errors (+mod_wsgi) using the Chrome Network panel

As a newcomer to Python, I am facing an issue with viewing error messages. The problem arises when a javascript function is making a call as follows: $.getJSON('test.py', {data: 'somedata'}, function(return){alert(return.echo)}) In th ...

Unable to locate module: '@material-ui/pickers' - Material UI React

I encountered an error that said: Material UI React - Module not found: Can't resolve '@material-ui/pickers' in React. Previously, I faced a similar issue with '@date-io/date-fns' but was able to fix it by updating to the latest ...

Retrieve data from a RESTful API

Can someone please explain the correct syntax for fetching an API with Node.js? I am trying to fetch it, but keep getting an error stating that 'fetch' is not defined. View image here ...

encountering an issue: "trigger new TypeError('JwtStrategy needs a secret or key');"

While attempting to run my web application, I encountered the following error: throw new TypeError('JwtStrategy requires a secret or key'); ^ TypeError: JwtStrategy requires a secret or key at new JwtStrategy (/Users/smitsanghvi/Des ...

Does the compression ratio for GZip change depending on the library used, such as Zlib in NodeJS and SharpZipLib in .Net?

Context: I am currently conducting tests on the compression ratio of our device. The data from the device is transmitted in the form of Json payloads, specifically in JArray format. I will be measuring the size of this data in Bytes before it undergoes com ...

"Having trouble with my for loop not functioning correctly with tkinter python menus. Can anyone offer guidance on how to fix

outputI am verifying that the label and command are functioning correctly. list = {'About', 'Experience'} comand = ['about','experience'] for i in range(len(list)): for t in range(len(comand)): help_menu ...

Error encountered with NG6 Angular sass files

Lately, I've been experimenting with NG6 Angular and it's growing on me. However, I hit a roadblock when attempting to switch from its default stylus to SASS, which is my preferred style in other projects. I have all the necessary dependencies in ...

Is there a way to transform my output into a list structure?

I am struggling to transform my result into the desired format: [5130, 5415, 5700, 5985, 6270, 6555, 6840, 7125, 7410, 7695, 7980] but I keep getting: 5130 5415 5700 5985 6270 6555 6840 7125 7410 7695 7980 I'm unsure how to make the conversion. The ...

How require works in Node.js

My current database connection module looks like this: var mongodb = require("mongodb"); var client = mongodb.MongoClient; client.connect('mongodb://host:port/dbname', { auto_reconnect: true }, function(err, db) { if (err) { ...

Is there a more effective way to write and enhance the custom Json string format?

In Python 2.7, I'm constructing a json string (result of an api call) that includes a list of unanswered threads. Currently, each thread is represented as an array element, and this setup has been functioning smoothly for me. However, in my quest to e ...

Tips for extracting parameters from a URL using Express JS in a custom manner

Recently, I set up a server using the express package and encountered an issue while trying to extract parameters from the URL in a specific format. The URL structure is as follows: (notice there's no '?' indicating parameters). I am lookin ...

Having trouble with installing packages through NPM in Node.js?

Whenever I try to install a package, NPM shows me an error like this: [............] /roolbackFailedOptional: verb npm-session .... followed by the final error message: npm ERR! code e503 npm ERR! 503 Service Unavailable: npm ERR! A complete log of th ...

What is the reason for the presence of two checks in a binary npm file

The content of every binary located in the node_modules/.bin directory is as follows: #!/bin/sh basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") case `uname` in *CYGWIN*) basedir=`cygpath -w "$basedir"`;; esac # Check if the ...

Encountering an issue while attempting to install bootstrap using npm

I'm facing difficulties while trying to set up bootstrap (LESS) using npm on Debian as the installation keeps failing. This is exactly what I am doing: git clone https://github.com/twbs/bootstrap.git npm install The error message I receive simply s ...

Can node.js handle the workload of a magazine or news website?

I have been impressed with the speed at which Node.js can build web apps, but I am curious about its performance when it comes to a magazine or news style website. Traditional CMS platforms like Joomla, Drupal, and WordPress are known for their effective ...

Pyinstaller is throwing an error message stating that it is unable to load AutoItX from the

When running my program from the command line, everything works perfectly fine. Now I need to create an Executable file, so I attempted to use pyinstaller. However, it seems that pyinstaller is unable to recognize the autoit module when automatically analy ...