Can't solve issue with "Cannot find module 'fs' [line 1]" in Code Node

I’m having trouble accessing the built‐in Node.js module fs in a Code node. Despite setting the environment variable to allow fs via my Docker setup—and even trying to extend the official image with a custom Dockerfile—I still get the error: Cannot find module ‘fs’

VMError: Cannot find module ‘fs’
at Resolver.resolveFull (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver.js:126:9)
at Resolver.resolve (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver.js:121:15)
at resolve (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/nodevm.js:317:21)
at VM2 Wrapper.apply (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/bridge.js:490:11)
… (stack trace continues)

const fs = require('fs');
const filePath = `/path/to/${$input.first().json.fileName}.md`;
const fileExists = fs.existsSync(filePath);
return [{ exists: fileExists }];
n8n version: 1.88.0 (Self Hosted)
Database (default: SQLite): SQLite
n8n EXECUTIONS_PROCESS setting (default: own, main): own
Running n8n via (Docker, npm, n8n cloud, desktop app): Docker (using an ARM64 container)
Operating system: Host OS is macOS; container is Linux/arm64

I initially ran the official image with:

docker run --platform linux/arm64 -it --rm --name n8n -p 5678:5678 \
  -e "NODE_FUNCTION_ALLOW_BUILTIN_MODULES=fs" \
  -v ~/n8n_data:/home/node/.n8n \
  -v /Users/REDACTED/data:/data \
  docker.n8n.io/n8nio/n8n:1.88.0

I verified via inspection that the environment variable appears in the container’s “Env” array.

When I still had the error I created a custom image build with this Dockerfile

FROM docker.n8n.io/n8nio/n8n:1.88.0
ENV NODE_FUNCTION_ALLOW_BUILTIN_MODULES=fs

But it didn’t solve the issue - I’m a bit stuck now since everything I’ve read tells me things SHOULD be working…

n8n’s Code node uses vm2 for sandboxing to prevent access to unsafe operations. So modules like:

  • fs
  • child_process
  • net

…are not available in Code nodes.

You can either use Execute Command node and return exists or missing.
Or you can use the Read File From Disk node (with option to Always Output Data and check if the output object is not empty.

1 Like

Appreciate the response, I wish this was better documented. Your advice seems to go against n8n’s own documentation: Enable modules in Code node | n8n Docs which I tried to follow leading me on a wild goose chase.

I’ll use Execute Command as suggested.

Cheers!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.