Unlock n8n: Enable External & Internal Libraries with Docker
Discover how to configure n8n in Docker to allow internal and external libraries securely on Ubuntu.

🐳 Docker & DevOps Implementation Guides
Complete Docker guides with optimization techniques, deployment strategies, and automation prompts to streamline your containerization workflow.
I hit this problem when running n8n inside Docker: the Code node wouldn’t let me import anything. Even basic modules like fs or popular npm libraries like node-fetch and moment threw errors such as “Module 'node-fetch' is disallowed”.
After debugging and reading through n8n’s docs, I found the exact way to enable both built-in Node modules and external npm packages securely, with persistence across restarts. Here’s the full setup that works on Ubuntu using Docker or Docker Compose.
Why This Happens
n8n runs Code nodes in a sandbox for security reasons. That sandbox blocks require() calls unless you explicitly allow them through environment variables.
There are two categories:
- Built-in modules (e.g.,
fs,path,crypto) - External npm modules (e.g.,
node-fetch,moment,axios)
Each must be whitelisted separately.
Step 1: Create a Persistent Volume
When you first run n8n, mount a named volume to store its data and dependencies.
docker volume create n8n_data
This volume persists your workflows, credentials, and any npm packages you install inside /home/node/.n8n.
Step 2: Create an Environment File
Instead of stuffing environment variables into a long command, use a .env file.
Create one at /home/matija/n8n.env:
GENERIC_TIMEZONE=CET
TZ=CET
WEBHOOK_URL=https://automate.we-hate-copy-pasting.com/
N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
N8N_RUNNERS_ENABLED=true
# Allow built-in Node modules
NODE_FUNCTION_ALLOW_BUILTIN=fs,crypto,path,fetch
# Allow external npm packages
NODE_FUNCTION_ALLOW_EXTERNAL=node-fetch,moment,lodash,axios
This file becomes your central configuration for Docker and Docker Compose.
Step 3: Start n8n with Docker
Run n8n with the .env file and your persistent volume:
docker run -d \
--name n8n \
-p 5678:5678 \
--env-file /home/matija/n8n.env \
-v n8n_data:/home/node/.n8n \
docker.n8n.io/n8nio/n8n
n8n will read all those variables automatically on startup.
Step 4: Install External Libraries Inside the Volume
External modules must exist in /home/node/.n8n/node_modules.
Install them once inside the running container:
docker exec -it n8n bash
cd /home/node/.n8n
npm install node-fetch moment lodash axios
exit
docker restart n8n
Because that directory lives inside the n8n_data volume, these packages stay installed even if you recreate the container.
Step 5: Verify the Configuration
Confirm n8n loaded your settings:
docker exec -it n8n printenv | grep NODE_FUNCTION
Expected output:
NODE_FUNCTION_ALLOW_BUILTIN=fs,crypto,path,fetch
NODE_FUNCTION_ALLOW_EXTERNAL=node-fetch,moment,lodash,axios
If both lines appear, the sandbox whitelist is active.
Step 6: Test Inside the Code Node
Check a Built-in Module
const fs = require('fs');
return fs.readdirSync('/');
If it lists files, built-ins are working.
Check an External Module
const moment = require('moment');
return {
iso: moment().toISOString(),
local: moment().format('YYYY-MM-DD HH:mm:ss')
};
You should see properly formatted timestamps.
Check node-fetch
const fetch = require('node-fetch');
const res = await fetch('https://api.github.com/repos/n8n-io/n8n');
return await res.json();
If you get structured JSON back, everything is configured correctly.
Step 7: Optional – Use Docker Compose
For a cleaner, repeatable setup, create a directory ~/n8n-docker and add these files.
File: ~/n8n-docker/docker-compose.yml
version: '3.8'
services:
n8n:
image: docker.n8n.io/n8nio/n8n
restart: unless-stopped
ports:
- "5678:5678"
env_file:
- ./n8n.env
volumes:
- n8n_data:/home/node/.n8n
volumes:
n8n_data:
external: true
File: ~/n8n-docker/n8n.env
(use the same contents as before)
Then run:
docker compose up -d
This reuses your existing n8n_data volume so all data and installed modules stay intact.
Step 8: Confirm Node’s Native Fetch (Optional)
If you’re using a newer n8n image (Node 18+), it includes native fetch.
You can test it without importing node-fetch:
const response = await fetch('https://api.github.com/repos/n8n-io/n8n');
const data = await response.json();
return data;
If that works, you can skip the shim entirely.
Otherwise, just keep using require('node-fetch').
Recap
You’ve now configured n8n to safely use both internal and external Node.js modules inside Code nodes, running in Docker on Ubuntu.
You learned how to:
- Persist data and npm modules via a Docker volume
- Whitelist allowed modules using environment variables
- Install and verify external npm packages
- Test everything inside n8n’s Code node
This setup makes it easy to add future libraries without breaking your sandbox.
Thanks, Matija