How to install npm packages for self hosted n8n deployments on Vultr?

Hi everyone,
I wanted to self host n8n on my Vultr machine .
I followed this tutorial https://youtu.be/f6J-MM0GVtw?si=2zj5U8yeaFJK0HN8 and now I have successfully self hosted n8n.

Currently I’m working on a workflow that involves the use of external npm packages but when I use the code node and do require(‘package-name’) , it says that package is not found. This is how n8n behaves by default.

How can I access npm packages on my self hosted Vultr based n8n deployment ?

I found a tutorial on Youtube in which the tutor was able to install npm packages on Docker based n8n deployment. I tried the same and my workflow works on that setup .

But I’ve deployed n8n on Vultr with PostgressDB inside a docker container ( Reference : https://youtu.be/f6J-MM0GVtw?si=2zj5U8yeaFJK0HN8 )

Has anyone come across a similar situation? Any help will be appreciated, thanks !

Hello gaurav.pant, welcome to n8n Community! I’ll try my best to help yaa.

I think you can use external npm packages in the code node. but you must install them into the n8n image and allow them via env vars. Here’’s a clean copy pasteable that you can try for a Docker/Vultr host.

First, create a custom image that includes your packages.

Dockerfile:

FROM n8nio/n8n:latest

# Install your packages globally (works well with Code node)
USER root
RUN npm install -g --unsafe-perm lodash dayjs axios
ENV NODE_PATH=/usr/local/lib/node_modules
USER node

Global install + NODE_PATH lets require(‘lodash’) resolve at runtime.

If its done, allow modules in the code node.

docker-compose.yml:

services:
  n8n:
    build: .
    ports:
      - "5678:5678"
    environment:
      - NODE_FUNCTION_ALLOW_BUILTIN=*       # optional: allow Node built-ins
      - NODE_FUNCTION_ALLOW_EXTERNAL=lodash,dayjs,axios
      # if you use queue mode / task runners, set these on workers too
    volumes:
      - n8n_data:/home/node/.n8n
    depends_on:
      - postgres

  postgres:
    image: postgres:15
    environment:
      - POSTGRES_USER=n8n
      - POSTGRES_PASSWORD=n8n
      - POSTGRES_DB=n8n
    volumes:
      - pg_data:/var/lib/postgresql/data

volumes:
  n8n_data:
  pg_data:

external modules mut be allowed with NODE_FUNCTION_ALLOW_EXTERNAL, and n8n will load them from the image’s node_modules.

and do:

docker compose build --no-cache
docker compose up -d

If its done again, use this in the code node:

const _ = require('lodash');
const dayjs = require('dayjs');
const axios = require('axios');

return [{
  json: {
    now: dayjs().toISOString(),
    uniq: _.uniq([1,1,2,3]),
  }
}];

Maybe this you can try, i hope this will work for you! :blush::raising_hands:

2 Likes

thanks @Naufal_Fayyadh , it worked !

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