Want to use npm modules in your django project without vendoring them? django-npm serves as a wrapper around the npm command-line program as well as a staticfiles finder.
pip install django-npm
- Make sure you have npm installed
- Make sure you have a
package.json
listing your dependencies - If you use a private registry, make sure your
.npmrc
is set up to connect to it - Add
npm.finders.NpmFinder
toSTATICFILES_FINDERS
- Set some paths in your project's
settings.py
./manage.py collectstatic
NPM_PREFIX_PATH
: absolute path to the npm "prefix' directory - this is where npm will look for yourpackage.json
, put yournode_modules
folder and look for a.npmrc
fileNPM_EXECUTABLE_PATH
: (optional) defaults to wherevernpm
is on your PATH. If you specify this, you can override the path to thenpm
executable. This is also an absolute path.NPM_DESTINATION_PREFIX
: (optional) Your npm files will end up under this path inside static. I usually use something like 'js/lib' (so your files will be in /static/js/lib/react.js for example) but you can leave it blank and they will just end up in the root.
By default, django-npm will expose all files in node_modules
to Django as staticfiles. You may not want all of them to be exposed. You can pick specific files by adding some additional configuration:
NPM_FILE_PATTERNS = {
'react': ['react.js'],
'express': ['lib/*.js', 'index.js']
}
Keys are the names of the npm modules, and values are lists containing strings. The strings match against glob patterns.
When you do a ./manage.py collectstatic
, django-npm will run npm install
for you and copy all the files into your STATIC_ROOT
.
- v0.1.4 - Fix bug with
NPM_EXECUTABLE_PATH
(thanks @yohanboniface) - v0.1.3 - Actually fix destination bug
- v0.1.2 - Fix bug with destination prefix
- v0.1.1 - manage.py runserver bugfix
- v0.1.0 - Add
NPM_FILE_PATTERNS
setting - v0.0.1 - initial release