How Comfy loads custom nodes

When Comfy starts, it scans the directory custom_nodes for Python modules, and attempts to load them. If the module exports NODE_CLASS_MAPPINGS, it will be treated as a custom node.

A python module is a directory containing an __init__.py file. The module exports whatever is listed in the __all__ attribute defined in __init__.py.

init.py

__init__.py is executed when Comfy attempts to import the module. For a module to be recognized as containing custom node definitions, it needs to export NODE_CLASS_MAPPINGS. If it does (and if nothing goes wrong in the import), the nodes defined in the module will be available in Comfy. If there is an error in your code, Comfy will continue, but will report the module as having failed to load. So check the Python console!

A very simple __init__.py file would look like this:

from .python_file import MyCustomNode
NODE_CLASS_MAPPINGS = { "My Custom Node" : MyCustomNode }
__all__ = ["NODE_CLASS_MAPPINGS"]

NODE_CLASS_MAPPINGS

NODE_CLASS_MAPPINGS must be a dict mapping custom node names (unique across the Comfy install) to the corresponding node class.

NODE_DISPLAY_NAME_MAPPINGS

__init__.py may also export NODE_DISPLAY_NAME_MAPPINGS, which maps the same unique name to a display name for the node. If NODE_DISPLAY_NAME_MAPPINGS is not provided, Comfy will use the unique name as the display name.

WEB_DIRECTORY

If you are deploying client side code, you will also need to export the path, relative to the module, in which the JavaScript files are to be found. It is conventional to place these in a subdirectory of your custom node named js.

Only .js files will be served; you can’t deploy .css or other types in this way

In previous versions of Comfy, __init__.py was required to copy the JavaScript files into the main Comfy web subdirectory. You will still see code that does this. Don’t.

ComfyUI Manager

While custom nodes can be installed manually, most people use ComfyUI Manager to install them. ComfyUI Manager takes care of installing, updating, and removing custom nodes, and any dependencies. But it isn’t part of the Comfy core, so you need to manually install it.

Installing ComfyUI Manager

cd ComfyUI/custom_nodes
git clone https://github.com/ltdrdata/ComfyUI-Manager.git

Restart Comfy afterwards. See ComfyUI Manager Install for details or special cases.

Using ComfyUI Manager

To make your custom node available through ComfyUI Manager you need to save it as a git repository (generally at github.com) and then submit a Pull Request on the ComfyUI Manager git, in which you have edited custom-node-list.json to add your node. More details.

When a user installs the node, ComfyUI Manager will:

1

Git Clone

git clone the repository,

2

Install Python Dependencies

install the pip dependencies listed in the custom node repository under requirements.txt (if present),

pip install -r requirements.txt
As is always the case with pip, it is possible that your node requirements will be in conflict with other custom nodes. Don’t make your requirements.txt any more restrictive than they need to be.
3

Run Install Script

execute install.py, if it is present in the custom node repository.

install.py is executed from the root path of the custom node

ComfyUI Manager files

As indicated above, there are a number of files and scripts that ComfyUI Manager will use to manage the lifecycle of a custom node. These are all optional.

  • requirements.txt - Python dependencies as mentioned above
  • install.py, uninstall.py - executed when the custom node is installed or uninstalled
    Users can just delete the directory, so you can’t rely on uninstall.py being run
  • disable.py, enable.py - executed when a custom node is disabled or re-enabled
    enable.py is only run when a disabled node is re-enabled - it should just reverse anything done in disable.py
    Disabled custom node subdirectory have .disabled appended to their names, and Comfy ignores these modules
  • node_list.json - only required if the custom nodes pattern of NODE_CLASS_MAPPINGS is not conventional.

See the ComfyUI Manager guide for official details.