DEV Community

codebangkok
codebangkok

Posted on

n8n: Code Node - Import external library (Python & JavaScript)

Reference: Task runners - Adding extra dependencies

1) Create file Dockerfile. You can extend the n8nio/runners image to add extra dependencies to the runners.

Example External Library
JavaScrip: moment uuid
Python: numpy requests

FROM n8nio/runners:stable
USER root
RUN cd /opt/runners/task-runner-javascript && pnpm add moment uuid
RUN cd /opt/runners/task-runner-python && uv pip install numpy requests
COPY n8n-task-runners.json /etc/n8n-task-runners.json
USER runner
Enter fullscreen mode Exit fullscreen mode

2) Create configuration file n8n-task-runners.json. You must also allowlist any first-party or third-party packages for use by the Code node.

NODE_FUNCTION_ALLOW_BUILTIN: comma-separated list of allowed node builtin modules.
NODE_FUNCTION_ALLOW_EXTERNAL: comma-separated list of allowed JS packages.
N8N_RUNNERS_STDLIB_ALLOW: comma-separated list of allowed Python standard library packages.
N8N_RUNNERS_EXTERNAL_ALLOW: comma-separated list of allowed Python packages.

{
  "task-runners": [
    {
      "runner-type": "javascript",
      "workdir": "/home/runner",
      "command": "/usr/local/bin/node",
      "args": [
        "--disallow-code-generation-from-strings",
        "--disable-proto=delete",
        "/opt/runners/task-runner-javascript/dist/start.js"
      ],
      "health-check-server-port": "5681",
      "allowed-env": [
        "PATH",
        "GENERIC_TIMEZONE",
        "NODE_OPTIONS",
        "N8N_RUNNERS_AUTO_SHUTDOWN_TIMEOUT",
        "N8N_RUNNERS_TASK_TIMEOUT",
        "N8N_RUNNERS_MAX_CONCURRENCY",
        "N8N_SENTRY_DSN",
        "N8N_VERSION",
        "ENVIRONMENT",
        "DEPLOYMENT_NAME",
        "HOME"
      ],
      "env-overrides": {
        "NODE_FUNCTION_ALLOW_BUILTIN": "crypto",
        "NODE_FUNCTION_ALLOW_EXTERNAL": "moment",
        "N8N_RUNNERS_HEALTH_CHECK_SERVER_HOST": "0.0.0.0"
      }
    },
    {
      "runner-type": "python",
      "workdir": "/home/runner",
      "command": "/opt/runners/task-runner-python/.venv/bin/python",
      "args": ["-m", "src.main"],
      "health-check-server-port": "5682",
      "allowed-env": [
        "PATH",
        "N8N_RUNNERS_LAUNCHER_LOG_LEVEL",
        "N8N_RUNNERS_AUTO_SHUTDOWN_TIMEOUT",
        "N8N_RUNNERS_TASK_TIMEOUT",
        "N8N_RUNNERS_MAX_CONCURRENCY",
        "N8N_SENTRY_DSN",
        "N8N_VERSION",
        "ENVIRONMENT",
        "DEPLOYMENT_NAME"
      ],
      "env-overrides": {
        "PYTHONPATH": "/opt/runners/task-runner-python",
        "N8N_RUNNERS_STDLIB_ALLOW": "*",
        "N8N_RUNNERS_EXTERNAL_ALLOW": "numpy,requests"
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

3) Build your custom image and use this image instead n8nio/runners

4) Test import external library in code node

Top comments (0)