Bindings and utils for integrating Node.js and NPM into a Django application.
from django_node import node, npm
from django_node.server import server
# Run a particular file with Node.js
stderr, stdout = node.run('/path/to/some/file.js', '--some-argument')
# Call `npm install` within a particular directory
stderr, stdout = npm.install('/path/to/some/directory')
# Add a persistent service to a Node server controlled by your python process
service = server.add_service('/some-endpoint', '/path/to/some/file.js')
# Pass data to your service and output the result
print(service(some_param=10, another_param='foo').text)pip install django-nodeAdd 'django_node' to your INSTALLED_APPS
INSTALLED_APPS = (
# ...
'django_node',
)The django_node.node module provides utils for introspecting and calling Node.js.
A method which will invoke Node.js with the arguments provided and return the resulting stderr and stdout.
Accepts an optional keyword argument, production, which will ensure that the command is run
with the NODE_ENV environment variable set to 'production'.
from django_node import node
stderr, stdout = node.run('/path/to/some/file.js', '--some-argument')
# With NODE_ENV set to production
stderr, stdout = node.run('/path/to/some/file.js', '--some-argument', production=True)
A method which will raise an exception if Node.js is not installed.
A method which will raise an exception if the installed version of Node.js is less than the version required.
Arguments:
version_required: a tuple containing the minimum version required.
from django_node import node
node_version_required = (0, 10, 0)
node.ensure_version_gte(node_version_required)A boolean indicating if Node.js is installed.
A tuple containing the version of Node.js installed. For example, (0, 10, 33)
A string containing the raw version returned from Node.js. For example, 'v0.10.33'
The django_node.npm module provides utils for introspecting and calling NPM.
A method that will invoke npm install in a specified directory. Optional arguments will be
appended to the invoked command.
Arguments:
target_dir: a string pointing to the directory which the command will be invoked in.*args: optional strings to append to the invoked command.silent: an optional boolean indicating that NPM's output should not be printed to the terminal.
from django_node import npm
# Install the dependencies in a particular directory
stderr, stdout = npm.install('/path/to/some/directory/')
# Install a dependency into a particular directory and persist it to the package.json file
stderr, stdout = npm.install('/path/to/some/directory/', '--save', 'some-package')
# Install dependencies but prevent NPM's output from being logged to the terminal
stderr, stdout = npm.install('/path/to/some/directory/', silent=True)A method which will invoke NPM with the arguments provided and return the resulting stderr and stdout.
from django_node import npm
stderr, stdout = npm.run('install', '--save', 'some-package')A method which will raise an exception if NPM is not installed.
A method which will raise an exception if the installed version of NPM is less than the version required.
Arguments:
version_required: a tuple containing the minimum version required.
from django_node import npm
npm_version_required = (2, 0, 0)
npm.ensure_version_gte(npm_version_required)A boolean indicating if NPM is installed.
A tuple containing the version of NPM installed. For example, (2, 0, 0)
A string containing the raw version returned from NPM. For example, '2.0.0'
Settings can be overridden by defining a dictionary named DJANGO_NODE in your settings file.
# Example
DJANGO_NODE = {
'PATH_TO_NODE': '/path/to/some/binary',
}A path that will resolve to Node.js.
Default:
'node'The command invoked on Node.js to retrieve its version.
Default:
'--version'A function which will generate a tuple of version numbers from the raw version string returned from Node.js.
Default
lambda version: tuple(map(int, (version[1:] if version[0] == 'v' else version).split('.')))A path that will resolve to NPM.
Default
'npm'The command invoked on NPM to retrieve its version.
Default
'--version'A function which will generate a tuple of version numbers from the raw version string returned from NPM.
Default
lambda version: tuple(map(int, version.split('.'))),The install command invoked on NPM. This is prepended to all calls to django_node.npm.install.
Default
'install'A path to a python interpreter which will be provided by NPM to any dependencies which require Python.
If you are using Python 3 as your system default or virtualenv python, NPM may throw errors
while installing certain libraries - such as gyp - which depend on Python 2.x. Specifying a
path to a Python 2.x interpreter should resolve these errors.
Default
Nonemkvirtualenv django-node
pip install -r requirements.txt
python runtests.py