django-npm-finder
Want to use npm modules in your django project without vendoring them?
django-npm-finder serves as a wrapper around the npm/yarn/pnpm cli and provides a staticfiles
finder that provides integration of your installed node_modules during development and hooks to
selectively provide collectstatic functionality to export static files for production without
exposing the entire node_modules hierarchy.
django-npm-finder is a fork of kevin1024's django-npm module with the
following changes:
- modern
pyproject.tomlpackaging (poetry) - caching of searches to provide better performance
- much of the core refactored to be more robust and predictable
- added support for alternative node managers, such as yarn and pnpm.
- autoconfigure default match patterns from
package.jsondependencies - added an extensive list of default ignore patterns
- unit tests were added (pytest)
These changes make the module easier to use, more reliable and as autoconfiguring as possible.
Installation
- Install into your django project
$ pip install django-npm-finder
$ poetry add django-npm-finder
$ uv pip install django-npm-finder
-
Install npm, yarn or pnpm. If you use a private registry, make sure your
.npmrcor equivalent is set up to connect to it. -
Have a
package.jsonat the root of your project (can be configured), listing your dependencies. -
Add
django_npm.finders.NpmFindertoSTATICFILES_FINDERS -
Configure your
settings.pyas detailed in the following section Configuration -
Run
$ ./manage.py npm_installfrom the command line, or with your own Python code (see npm install section below). Or install your npm modules using[p]npm|yarn installfrom the command line. -
$ ./manage.py collectstaticwill copy all selected node_modules files into yourSTATIC_ROOT. This is only required at deployment, and if using Django runserver for development, will not be required.
Configuration
In the following section, reference to npm also includes yarn or pnpm.
-
NPM_ROOT_PATH: path to the npm "root" directory, where your package manager will look forpackage.json,node_modules,.npmrcetc. Set this if it differs from the root of your Django project (usuallysettings.BASE_DIRin most Django projects). -
NPM_EXECUTABLE_PATH: (optional, default manager) setsnpmas modules manager and optionally overrides its location. Supported NPM managers are: npm, yarn and pnpm. -
NPM_STATIC_FILES_PREFIX: (optional) Your npm files will be located under this path inside the static URL. As an example, if set to 'vendor' the collected files will be located in/static/vendor/dist/bootstrap.min.js, and if not set, it will be located in/static/dist/bootstrap.min.js. -
NPM_FILE_PATTERNS: (optional) By default, django-npm will expose all modules defined as dependencies inpackage.jsonto Django as staticfiles, excluding files using the default or specified ignore patterns. You may want to restrict what is exposed. You can pick specific files by adding some additional configuration shown in the following code block. Keys are the names of the npm modules, and values are lists containing strings. The strings match against glob patterns. Use double asterisk wildcard '**' to include all subdirectories.NOTE: If unset, the module will autoconfigure modules based on the dependencies specified in
package.json.
NPM_FILE_PATTERNS = {
'bootstrap': ['dist/*'],
'htmx.org': ['htmx.org/dist/*']
}
-
NPM_IGNORE_PATTERNS: (optional) This is a python list of patterns to exclude. There is an extensive list of default patterns that are excluded, but you can override this. -
NPM_FINDER_USE_CACHE: (default True) A boolean that enables cache in the finder. If enabled, the file list will be computed only once when the server is started.
npm install
To add the ./manage.py npm_install "django_npm" must be added to Django's INSTALLED_APPS setting, otherwise it doesn't need to be added there.
Even if the module is not added in INSTALLED_APPS you can run npm install programmatically
from python by creating a script as follows:
from django_npm.finders import npm_install
npm_install()
The advantage of using npm_install is that it will run the package manager configured in
your Django settings.
Metadata
Release files for django-npm-finder 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_npm_finder-1.3.0.tar.gz | 6.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_npm_finder-1.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.0 kB
Release files / django_npm_finder-1.3.0.tar.gz
| Download URL | django_npm_finder-1.3.0.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1d5eb08bd2ca31a11aae079b7c11d0afda73ec2b4f2cce252e6dfd475e6c4d8a
|
|
BLAKE2b-256 checksum How to use checksums |
caf2f0b464540e70fd7ff544c5e69fad3a1a237c0824bc7626d811ddaf939aa8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / django_npm_finder-1.3.0-py3-none-any.whl
| Download URL | django_npm_finder-1.3.0-py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
20585fd9afedac8ddeb02296f0ad4fa46f9d929f1df8b5cf46bbe6e817184a4e
|
|
BLAKE2b-256 checksum How to use checksums |
08f3c436d9060eaa387c13e1df2077e90a4abe03f5b78bd4c8d783a5e6e4eab2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|