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:
- 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, thehttpRequest
node still failed withJSON parameter needs to be valid JSON
. We confirmed the generated JSON was valid and worked in Postman, pointing to an issue withhttpRequest
’s internal handling of large Base64 payloads in JSON in this N8N version/environment. - 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. - Attempt 1 (Code Node with
axios
): Used code involvingconst axios = require('axios');
. Error:Cannot find module 'axios'
. - Attempt 2 (Code Node with
https
): Used code involvingconst https = require('https');
. Error:Cannot find module 'https'
. - 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 proveshttps
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 provesaxios
is installed in the container.
- 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. - 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 theenvironment:
block of then8n
service in thedocker-compose.yml
file. Redeployed the stack withdocker stack deploy
. - 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
- Final Attempt (Code Node with
https
oraxios
): After confirming the variables are correctly loaded in the container, I re-tested the Code node with both thehttps
andaxios
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
andNODE_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!