Skip to main content

A django staticfiles finder that supporting npm/yarn/pnpm

Project description

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.toml packaging (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.json dependencies
  • 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 .npmrc or equivalent is set up to connect to it.

  • Have a package.json at the root of your project (can be configured), listing your dependencies.

  • Add django_npm.finders.NpmFinder to STATICFILES_FINDERS

  • Configure your settings.py as detailed in the following section Configuration

  • Run $ ./manage.py npm_install from the command line, or with your own Python code (see npm install section below). Or install your npm modules using [p]npm|yarn install from the command line.

  • $ ./manage.py collectstatic will copy all selected node_modules files into your STATIC_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 for package.json, node_modules, .npmrc etc. Set this if it differs from the root of your Django project (usually settings.BASE_DIR in most Django projects).

  • NPM_EXECUTABLE_PATH: (optional, default manager) sets npm as 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 in package.json to 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_npm_finder-1.3.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_npm_finder-1.3.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file django_npm_finder-1.3.0.tar.gz.

File metadata

  • Download URL: django_npm_finder-1.3.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for django_npm_finder-1.3.0.tar.gz
Algorithm Hash digest
SHA256 1d5eb08bd2ca31a11aae079b7c11d0afda73ec2b4f2cce252e6dfd475e6c4d8a
MD5 13ef0fa134c21b36f2b0bf87e714f7f6
BLAKE2b-256 caf2f0b464540e70fd7ff544c5e69fad3a1a237c0824bc7626d811ddaf939aa8

See more details on using hashes here.

File details

Details for the file django_npm_finder-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: django_npm_finder-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for django_npm_finder-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 20585fd9afedac8ddeb02296f0ad4fa46f9d929f1df8b5cf46bbe6e817184a4e
MD5 2641ada0135a44907c6186e1386b24ba
BLAKE2b-256 08f3c436d9060eaa387c13e1df2077e90a4abe03f5b78bd4c8d783a5e6e4eab2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page