Add npm package in docker-compose

Describe the problem/error/question

I am trying to install this npm package (@tryfabric) and use it in a “code” node in NPM. I have created a dockerfile, added it to docker-compose.yml, built docker image, but still get “Module_not_found” and VM error when trying to access the package in the code node.

I tried both installing the package globally and in the directory of the node_modules. I do see that the @tryfabric module is present there.

What is the error message (if any)?

node
Uncaught ReferenceError: node is not defined
require(‘@tryfabric/martian’);
Uncaught Error: Cannot find module ‘@tryfabric/martian’
Require stack:

  • at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15) at Module._load (node:internal/modules/cjs/loader:922:27) at Module.require (node:internal/modules/cjs/loader:1143:19) at require (node:internal/modules/cjs/helpers:119:18) { code: 'MODULE_NOT_FOUND', requireStack: [ '' ]

Please share your workflow

Share the output returned by the last node

ERROR: Cannot find module ‘@tryfabric/martian’ [line 1]
VMError

Tried this Dockerfile content:

Dockerfile

USER root

RUN npm install -g @tryfabric/martian

USER node

Separately tried this dockerfile too:

GNU nano 6.2 dockerfile
FROM n8nio/n8n:latest

USER root

Change the working directory

WORKDIR /usr/local/lib/node_modules/n8n

Install the module in the local n8n node_modules directory

RUN npm install @tryfabric/martian

Revert to the default working directory

WORKDIR /data

Switch back to the node user for security purposes

USER node

Docker-compose.yml

@tryfabric module is present in this directory: /usr/local/lib/node_modules/n8n/node_modules/@tryfabric

This is how I build the docker image:

Stop current setup

sudo docker-compose stop

Delete it (will only delete the docker-containers, data is stored separately)

sudo docker-compose rm

Pull the (latest) image not Next (to do that specify the version in docker-compose.yml

sudo docker build --no-cache -t dockerfile .

Then start it again

sudo docker-compose up -d

Information on your n8n setup

  • **n8n version:**1.22.3
  • **Running n8n via (Docker, npm, n8n cloud, desktop app):**Docker

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

Hi @pooria, to use external modules in n8n you’d need to set the NODE_FUNCTION_ALLOW_EXTERNAL environment variable.

I’ve given this a quick go on my side and could run the following workflow as expected:

Result:

This was the exact Dockerfile I have used:

FROM n8nio/n8n:1.24.1
USER root
RUN npm install -g @tryfabric/martian
USER node

This was my docker-compose.yml:

services:
  n8n:
    build: .
    environment:
      - NODE_FUNCTION_ALLOW_EXTERNAL=*
    volumes:
      - ./data:/home/node/.n8n
    ports:
      - 5678:5678

After running docker compose build and docker compose up my n8n instance started as expected and I could run the above example workflow.

Hope this helps!