How To Install External Module (Library) for Code Node (NodeJS) Without Touching Dockerfile?

Information on your n8n setup

  • n8n version: 1.104.1
  • Database (default: SQLite): Postgres
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: 22.04

Hello.

I’m trying to load up the library pdf-lib for my Code node. What I did:

  1. Change the Docker environment: NODE_FUNCTION_ALLOW_EXTERNAL=pdf-lib
  2. SSH into the n8n container: docker exec -it n8n sh
  3. Install the library: cd ~/.n8n/nodes && npm install pdf-lib - This does not work. Still library not found.
  4. Install the library in a different location: cd /usr/local/lib && npm install pdf-lib - I run into permission problems.

So, how exactly do I add the library to node_modules so it’s available for my code node?

CLI:

$ docker exec -it --user root n8n /bin/sh
/home/node # cd .n8n/
/home/node/.n8n # npm install -g hello-world-npm

added 1 packages in 620ms
/home/node/.n8n # exit

environment:

- NODE_FUNCTION_ALLOW_EXTERNAL=hello-world-npm

then in the code node:

var hw = require('hello-world-npm');

return [
  {
    text: hw()
  }
]

results in

1 Like

This works! I didn’t think about exec as root and installing the package globally.

Thank you!

1 Like

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