Code/Function Node "Cannot find module" despite correct ENV vars and module presence - v1.91.3 Docker Swarm

N8N Version: 1.91.3 (Self Hosted) Installation Method: Docker Swarm, deployed via docker stack deploy using a docker-compose.yml file. Operating System: (Mention your VPS OS, e.g., Ubuntu 22.04)

The Goal: I am trying to make an HTTP POST request to an external API (Google Gemini Vision) from a N8N Code/Function node. The request body needs to be JSON containing a Base64 encoded image.

The Problem: The Code node consistently fails with a “Cannot find module” error, specifically for core Node.js modules like https and external libraries like axios, even though they are confirmed to be available in the container and the relevant environment variables are set correctly.

Debugging Steps & Findings:

  1. Initial Attempt (httpRequest node): Tried using the built-in httpRequest node with a JSON body containing the Base64 image data (generated by a previous node). This failed initially due to Docker Swarm networking/port conflicts which were resolved. Once Docker was working, the httpRequest node still failed with JSON parameter needs to be valid JSON. We confirmed the generated JSON was valid and worked in Postman, pointing to an issue with httpRequest’s internal handling of large Base64 payloads in JSON in this N8N version/environment.
  2. Switched to Code Node: Decided to use a Code node to make the HTTP request programmatically, believing it might bypass the httpRequest node’s issue.
  3. Attempt 1 (Code Node with axios): Used code involving const axios = require('axios');. Error: Cannot find module 'axios'.
  4. Attempt 2 (Code Node with https): Used code involving const https = require('https');. Error: Cannot find module 'https'.
  5. Container Investigation (docker exec sh): To understand why modules weren’t found, I accessed the running N8N container.
  • Checked Node.js version: node -v showed v20.19.0.
  • Tested built-in modules: node -e "require('https'); console.log('HTTPS found!')" SUCCEEDED and printed “HTTPS found!”. This proves https is available in the container’s main Node.js environment.
  • Tested external library presence: find /usr/local /app /opt -name axios -type d 2>/dev/null SUCCEEDED and found directories like /usr/local/lib/node_modules/n8n/node_modules/axios. This proves axios is installed in the container.
  1. Environment Variable Check (Inside Container): Realized the Code node’s sandbox might be restricted by environment variables. Initially, env | grep NODE_FUNCTION showed no output, indicating variables weren’t loading from the .env file.
  2. Corrected ENV Variable Loading: Moved the necessary environment variables (NODE_FUNCTION_ALLOW_BUILTIN, NODE_FUNCTION_ALLOW_EXTERNAL, GEMINI_API_KEY) from the .env file directly into the environment: block of the n8n service in the docker-compose.yml file. Redeployed the stack with docker stack deploy.
  3. Final ENV Variable Check (Inside New Container): Accessed the new container and ran env | grep again. This time, the variables WERE correctly loaded: NODE_FUNCTION_ALLOW_EXTERNAL=axios NODE_FUNCTION_ALLOW_BUILTIN=https,crypto,vm,buffer,querystring,url,fs,path GEMINI_API_KEY=MY_API_KEY_IS_HERE
  4. Final Attempt (Code Node with https or axios): After confirming the variables are correctly loaded in the container, I re-tested the Code node with both the https and axios code snippets. Both still give the “Cannot find module” error.

Conclusion:

  • The required modules (https, axios) are confirmed to be available in the N8N container’s Node.js environment.
  • The environment variables NODE_FUNCTION_ALLOW_BUILTIN and NODE_FUNCTION_ALLOW_EXTERNAL are confirmed to be correctly loaded into the container’s environment.
  • However, the Code node’s sandbox (VM2) is still unable to find/access these modules, throwing “Cannot find module”.

This indicates a likely issue (bug or specific configuration conflict) within the N8N v1.91.3 sandbox implementation that prevents it from correctly honoring the environment variables or accessing the modules in the container’s environment.

docker-compose.yml:

version: “3”

services:
n8n:
image: n8nio/n8n:latest
# … other options …
environment:
- N8N_HOST=n8n.electromedicos.com
- N8N_PORT=5678
- NODE_FUNCTION_ALLOW_BUILTIN=https,crypto,vm,buffer,querystring,url,fs,path
- NODE_FUNCTION_ALLOW_EXTERNAL=axios
- GEMINI_API_KEY=MY_SECRET_API_KEY # Replaced with placeholder
labels:
- “traefik.enable=true”
- “traefik.http.routers.n8n.rule=Host(n8n.electromedicos.com)”
- “traefik.http.services.n8n.loadbalancer.server.port=5678”
networks:
- web

networks:
web:
external: true

Error Stack Trace (from Code node):

{ "errorMessage": "Cannot find module 'https' [line 2]", // Or 'axios' [line 1] "errorDescription": "VMError", "errorDetails": {}, "n8nDetails": { "nodeName": "Code1", "nodeType": "n8n-nodes-base.code", "nodeVersion": 2, "n8nVersion": "1.91.3 (Self Hosted)", // ... rest of stack trace showing @n8n/vm2 ... } }

Has anyone encountered this issue with the Code/Function node in N8N v1.91.3 (or similar versions) where NODE_FUNCTION_ALLOW_BUILTIN or NODE_FUNCTION_ALLOW_EXTERNAL don’t seem to work correctly? Is there a known bug, a different required configuration (especially in a Docker Swarm context), or another way to make standard modules/libraries like https and axios available in the sandbox?

Any help or insight would be greatly appreciated!

I believe the packages need to be installed in the “data dir”. For example my docker file below volumes to /home/node/.n8n, so you should install axios to this dir: /home/node/.n8n/nodes. This is the same place community nodes gets installed to.

  n8n:
    image: docker.n8n.io/n8nio/n8n:latest
    container_name: n8n
    restart: always
    depends_on:
      - n8n-db
    volumes:
      - /var/containers/volumes/n8n/app-data:/home/node/.n8n