Enabling Axios in n8n Code Nodes: Complete Setup Guide

Last updated:

Home » Articles » Web Development » Enabling Axios in n8n Code Nodes: Complete Setup Guide

By default, n8n Code nodes cannot make HTTP requests or access external modules like Axios. This limitation exists for security reasons, but you can enable external modules in self-hosted n8n instances by configuring the NODE_FUNCTION_ALLOW_EXTERNAL environment variable.

⚠️ Important Security Note

Enabling external modules allows Code nodes to make arbitrary HTTP requests and access npm packages. Only enable this on trusted n8n instances where you control who can create workflows. Consider implementing network restrictions and access controls.

Prerequisites

  • Self-hosted n8n instance (this doesn’t work on n8n Cloud)
  • Docker or direct npm installation
  • SSH access to your server

Step 1: Identify Your n8n Setup

First, determine how your n8n instance is running:

# Check for running containers and processes
ps aux | grep -E "(n8n|docker|pm2)"

Common setups:

  • Docker Compose: You’ll see docker processes and containers
  • Direct npm: You’ll see node processes running n8n
  • PM2: You’ll see PM2 managing n8n processes

Step 2: Find Your Configuration

For Docker Compose Setups

# Locate your docker-compose.yml file
find / -name "docker-compose.yml" -o -name "docker-compose.yaml" 2>/dev/null

# View the current configuration
cat /path/to/your/docker-compose.yml

For npm/PM2 Setups

# Check for environment files
ls -la ~/.n8n/.env
ls -la /etc/systemd/system/n8n.service

Step 3: Add the Environment Variable

Choose the method that matches your setup from Step 1. You only need to follow ONE of these paths:

Path A: Docker Compose Setup (✅ Tested & Confirmed Working)

If you have docker containers and found a docker-compose.yml file:

Edit your docker-compose.yml file and add NODE_FUNCTION_ALLOW_EXTERNAL=axios to the environment section of both the main n8n service and any worker services:

services:
  n8n:
    image: docker.n8n.io/n8nio/n8n
    environment:
      - N8N_HOST=your-domain.com
      - N8N_PORT=5678
      # ... other environment variables
      - NODE_FUNCTION_ALLOW_EXTERNAL=axios
      # ... rest of environment variables

  n8n-worker:  # If you have workers
    image: n8nio/n8n
    command: worker
    environment:
      - EXECUTIONS_MODE=queue
      # ... other environment variables
      - NODE_FUNCTION_ALLOW_EXTERNAL=axios
      # ... rest of environment variables

Key points:

  • Add the variable to both main n8n and worker containers
  • Maintain proper YAML indentation (6 spaces before the dash)
  • Keep it with other environment variables

Path B: Direct npm Installation

If you see node processes running n8n directly (no Docker):

# Option 1: Add to .env file
echo "NODE_FUNCTION_ALLOW_EXTERNAL=axios" >> ~/.n8n/.env

# Option 2: Export in shell profile
export NODE_FUNCTION_ALLOW_EXTERNAL=axios

# Option 3: Start n8n with the variable
NODE_FUNCTION_ALLOW_EXTERNAL=axios n8n start

Path C: PM2 Process Manager

If you see PM2 managing n8n processes, edit your ecosystem.config.js:

module.exports = {
  apps: [{
    name: 'n8n',
    script: 'n8n',
    args: 'start',
    env: {
      NODE_FUNCTION_ALLOW_EXTERNAL: 'axios'
    }
  }]
};

Step 4: Restart Your n8n Instance

Choose the restart method that matches your setup:

Path A: Docker Compose (✅ What We Used Successfully)

cd /path/to/your/compose/directory

# Method 1: Using docker compose (newer syntax)
docker compose down
docker compose up -d

# Method 2: Using docker-compose (older syntax)
docker-compose down
docker-compose up -d

# Method 3: Simple reboot (if unsure about Docker commands)
reboot

Path B: Direct npm Installation

# Stop n8n and restart with environment variable
pkill -f n8n
NODE_FUNCTION_ALLOW_EXTERNAL=axios n8n start

# Or if using systemd service
sudo systemctl restart n8n

Path C: PM2 Process Manager

# Restart PM2 with new configuration
pm2 restart ecosystem.config.js

Step 5: Verify the Configuration

Check if the environment variable is properly set in running containers:

# For Docker setups – check main container
docker exec -it $(docker ps | grep n8n | grep -v worker | awk '{print $1}') env | grep NODE_FUNCTION

# Check worker container (if applicable)
docker exec -it $(docker ps | grep worker | awk '{print $1}') env | grep NODE_FUNCTION

You should see:

NODE_FUNCTION_ALLOW_EXTERNAL=axios

Step 6: Test Axios in a Code Node

Create a new Code node in n8n with this test code:

// Test if axios is available
const axios = require('axios');
console.log('✅ Axios available:', typeof axios);

// Simple test request (replace with your endpoint)
const domain = $input.first().json.domain || 'https://httpbin.org/get';

// Fire-and-forget request example
axios.get(domain, {
  timeout: 1000
}).then(response => {
  console.log('Request successful');
}).catch(error => {
  console.log('Request completed (errors ignored for fire-and-forget)');
});

// Continue workflow immediately
return $input.all();

Troubleshooting

“Cannot find module ‘axios'” Error

  1. Check environment variable: Verify it’s set in running containers
  2. Restart required: Environment changes need container/process restart
  3. YAML formatting: Ensure proper indentation in docker-compose.yml
  4. Worker containers: Make sure workers also have the environment variable

Docker Compose Issues

# If docker-compose command not found
apt install docker-compose

# Force restart all containers
docker stop $(docker ps -q)
docker compose up -d

Still Not Working?

  1. Check logs: docker-compose logs n8n
  2. Verify file syntax: docker-compose config
  3. Try manual restart: reboot (simple but effective)

Security Best Practices

  1. Limit modules: Only allow specific modules you need
NODE_FUNCTION_ALLOW_EXTERNAL=axios,lodash,moment
  1. Network restrictions: Use firewall rules to limit outbound connections
  2. Access control: Restrict who can create/edit workflows with Code nodes
  3. Monitor requests: Log HTTP requests from Code nodes
  4. Regular updates: Keep axios and n8n updated for security patches

Alternative: Using HTTP Request Node

If security is a major concern, consider using n8n’s built-in HTTP Request node instead:

  • Pros: More secure, built-in functionality, no external modules needed
  • Cons: Less flexibility than axios, harder to implement fire-and-forget patterns

For fire-and-forget behavior with HTTP Request node:

  • Set response format to “Ignore”
  • Enable “Never Error” option
  • Set short timeout (1-2 seconds)

Conclusion

Enabling axios in n8n Code nodes requires careful configuration of environment variables and proper restart procedures. While it increases functionality, always consider the security implications and implement appropriate safeguards for your use case.

📋 Key Steps Summary

  1. Add NODE_FUNCTION_ALLOW_EXTERNAL=axios to your configuration
  2. Ensure it’s applied to both main and worker containers/processes
  3. Restart your n8n instance completely
  4. Verify the environment variable is set in running containers
  5. Test with a simple Code node

Remember: this only works with self-hosted n8n instances, not n8n Cloud.

Wave

Enjoy our articles? Join our free list and get more.

Sign Up

Book Discovery Call