Their docs and forums suck to be specific about this.
Install specific packages inside the n8n Docker container and then use it in its nodes:
# Use the official n8n image as the base
FROM n8nio/n8n:1.82.4
# Switch to root user to install dependencies
USER root
# Install the npm packages globally so they're accessible to n8n
RUN npm install -g axios dotenv
# Set environment variables for n8n nodes
# @link: https://docs.n8n.io/hosting/configuration/configuration-examples/modules-in-code-node/
ENV NODE_FUNCTION_ALLOW_BUILTIN=*
# Comma separated list of npm packages to use inside n8n
ENV NODE_FUNCTION_ALLOW_EXTERNAL=axios,dotenv
# Switch back to the n8n default user
USER node
# Run n8n using the default entrypoint sh file
ENTRYPOINT ["tini", "--", "/docker-entrypoint.sh"]
Inside a "Code" node in n8n try this (use a real URL):
const axios = require('axios');
async function fetchData() {
const response = await axios.get('https://api.yourdomain.com/ping');
return [{ json: response.data }];
}
return fetchData();
It should return the data and throw no error.