---
title: "Unlock n8n: Enable External & Internal Libraries with Docker"
slug: "enable-external-internal-libraries-n8n-docker-ubuntu"
published: "2025-11-08"
updated: "2025-11-12"
categories:
  - "Docker"
tags:
  - "n8n"
  - "Docker"
  - "Ubuntu"
  - "enable libraries in n8n"
  - "n8n Docker setup"
  - "node-fetch"
  - "npm modules"
  - "Code node configuration"
  - "external npm packages"
  - "built-in Node modules"
llm-intent: "reference"
audience-level: "intermediate"
framework-versions:
  - "n8n@latest"
  - "node@18+"
  - "docker@24+"
  - "docker-compose@2+"
status: "stable"
llm-purpose: "Unlock the power of n8n by enabling internal and external libraries using Docker on Ubuntu. Start enhancing your workflows today!"
llm-prereqs:
  - "Access to n8n"
  - "Access to Docker"
  - "Access to Docker Compose"
  - "Access to npm"
llm-outputs:
  - "Updated docker-compose configuration files for the new environment"
---

**Summary Triples**
- (Unlock n8n: Enable External & Internal Libraries with Docker, focuses-on, Unlock the power of n8n by enabling internal and external libraries using Docker on Ubuntu. Start enhancing your workflows today!)
- (Unlock n8n: Enable External & Internal Libraries with Docker, category, general)

### {GOAL}
Unlock the power of n8n by enabling internal and external libraries using Docker on Ubuntu. Start enhancing your workflows today!

### {PREREQS}
- Access to n8n
- Access to Docker
- Access to Docker Compose
- Access to npm

### {STEPS}
1. Create a Persistent Volume
2. Create an Environment File
3. Start n8n with Docker
4. Install External Libraries
5. Verify the Configuration
6. Test Inside the Code Node
7. Use Docker Compose (Optional)
8. Confirm Node’s Native Fetch (Optional)

<!-- llm:goal="Unlock the power of n8n by enabling internal and external libraries using Docker on Ubuntu. Start enhancing your workflows today!" -->
<!-- llm:prereq="Access to n8n" -->
<!-- llm:prereq="Access to Docker" -->
<!-- llm:prereq="Access to Docker Compose" -->
<!-- llm:prereq="Access to npm" -->
<!-- llm:output="Updated docker-compose configuration files for the new environment" -->

# Unlock n8n: Enable External & Internal Libraries with Docker
> Unlock the power of n8n by enabling internal and external libraries using Docker on Ubuntu. Start enhancing your workflows today!
Matija Žiberna · 2025-11-08

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.

```bash
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`:

```bash
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:

```bash
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:

```bash
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:

```bash
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

```js
const fs = require('fs');
return fs.readdirSync('/');
```

If it lists files, built-ins are working.

### Check an External Module

```js
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`

```js
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`**

```yaml
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:

```bash
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`:

```js
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

## LLM Response Snippet
```json
{
  "goal": "Unlock the power of n8n by enabling internal and external libraries using Docker on Ubuntu. Start enhancing your workflows today!",
  "responses": [
    {
      "question": "What does the article \"Unlock n8n: Enable External & Internal Libraries with Docker\" cover?",
      "answer": "Unlock the power of n8n by enabling internal and external libraries using Docker on Ubuntu. Start enhancing your workflows today!"
    }
  ]
}
```