I’m running into a very stubborn issue on my self-hosted n8n instance and I’m hoping someone might have an idea what’s going on, as I’ve exhausted all the usual troubleshooting steps.
My Goal
I’m trying to use the cheerio library within a Code node to parse HTML content retrieved from a web scrape.
The Problem
When I run my workflow, the Code node fails on the very first effective line with the error: VMError: Cannot find module 'cheerio' [line 2, for item 0]
Code Snippet:
The code in the node is very simple and fails immediately on the require statement:
JavaScript
// Load the cheerio library to parse HTML
const cheerio = require('cheerio');
// ... rest of the code
What I’ve Already Tried
Here is a log of all the steps I’ve taken so far.
Set Environment Variable: I’ve edited my docker-compose.yml file to include the necessary environment variable. My environment section looks like this:
Restarted Docker Compose: I’ve restarted the stack using docker-compose up -d --force-recreate. I also tried the docker-compose down and docker-compose up -d cycle to resolve an intermediate error with the postgres container.
Verified the Environment Variable: I have confirmed that the variable is correctly set inside the running n8n container. Running docker exec -it <container_name> env | grep N8N_NODE_MODULES_INCLUDE successfully returns:
N8N_NODE_MODULES_INCLUDE=cheerio
Checked Logs: I have checked the startup logs using docker logs <container_name> and there are no errors related to npm, network issues, or permissions during the module installation phase. The logs show a clean startup.
Forced Re-installation: I forced a completely fresh installation of the modules by:
Getting a shell inside the container (docker exec -it <container_name> /bin/sh).
Deleting the cached modules folder (rm -rf /home/node/.n8n/nodes).
Restarting the container.
Even after all of these steps, the error persists. It seems like the n8n instance has the variable set correctly and experiences no errors during startup, but the Code node’s execution environment is still unable to find the module.
Has anyone encountered such a stubborn issue before, or have any ideas what else I could check? Any help would be greatly appreciated!
I tried replacing N8N_NODE_MODULES_INCLUDE=cheerio with NODE_FUNCTION_ALLOW_EXTERNAL=cheerio, but unfortunately, the error Cannot find module 'cheerio' still persists.
To give you and the community the full picture, this has been an extremely stubborn problem. I have tried every possible diagnostic step I can think of, and I’m hoping someone might spot what I’m missing.
Here is a complete summary of everything I have done so far:
Upgraded n8n: I started on version 1.105.4 and have now successfully upgraded to 1.106.3 to rule out any old bugs. The error persists on the new version.
Tried All Environment Variables:
I started with N8N_NODE_MODULES_INCLUDE=cheerio.
I tried your suggestion of NODE_FUNCTION_ALLOW_EXTERNAL=cheerio.
After upgrading, I am now using the modern, correct variable: NODE_FUNCTION_ALLOW_BUILTIN=cheerio.
Verified the Variable is Set: I have used docker exec to get a shell inside the running n8n container and confirmed the variable is present and correct. The command docker exec <container> env | grep BUILTIN successfully returns NODE_FUNCTION_ALLOW_BUILTIN=cheerio.
Checked Startup Logs: The docker logs for the n8n container are completely clean. There are no errors related to npm, network issues, or permissions during the module installation phase. It reports “n8n ready” successfully.
Forced Re-installation: I have forced a completely fresh installation of the modules by:
Getting a shell inside the container (docker exec).
Deleting the cached modules folder (rm -rf /home/node/.n8n/nodes).
Restarting the stack (docker compose down and up).
It feels like the issue must be with the host server environment (filesystem permissions, a specific Docker daemon setting, etc.), but I have no idea what to check.
Does anyone have any ideas for what could cause this?