How to install node packages, where's the node_modules?

how to install node packages, where’s the node_modules?
I’d like to use external library from node modules like sanitize-html module.
Please guide me on how to make it work as it doesn’t work now.

Thanks!

version: "3.7"

services:
  n8n_2024:
    container_name: aladdin_2024
    image: docker.n8n.io/n8nio/n8n
    restart: always
    ports:
      - "127.0.0.1:7182:5678"
    labels:
      - traefik.enable=true
      - traefik.http.routers.n8n_2024.rule=Host(`${SUBDOMAIN}.${DOMAIN_NAME}`)
      - traefik.http.routers.n8n_2024.tls=true
      - traefik.http.routers.n8n_2024.entrypoints=web,websecure
      - traefik.http.routers.n8n_2024.tls.certresolver=mytlschallenge
      - traefik.http.middlewares.n8n_2024.headers.SSLRedirect=true
      - traefik.http.middlewares.n8n_2024.headers.STSSeconds=315360000
      - traefik.http.middlewares.n8n_2024.headers.browserXSSFilter=true
      - traefik.http.middlewares.n8n_2024.headers.contentTypeNosniff=true
      - traefik.http.middlewares.n8n_2024.headers.forceSTSHeader=true
      - traefik.http.middlewares.n8n_2024.headers.SSLHost=${DOMAIN_NAME}
      - traefik.http.middlewares.n8n_2024.headers.STSIncludeSubdomains=true
      - traefik.http.middlewares.n8n_2024.headers.STSPreload=true
      - traefik.http.routers.n8n_2024.middlewares=n8n_2024@docker,corsheaders@docker
      - traefik.http.middlewares.corsheaders.headers.accesscontrolallowheaders=Content-Type,Authorization
      - traefik.http.middlewares.corsheaders.headers.accesscontrolallowcredentials=true
      - traefik.http.services.n8n_2024.loadbalancer.server.port=7182
    environment:
      - N8N_BASIC_AUTH_ACTIVE=true
      - N8N_BASIC_AUTH_USER
      - N8N_BASIC_AUTH_PASSWORD
      - N8N_HOST=${SUBDOMAIN}.${DOMAIN_NAME}
      - N8N_PORT=7182
      - N8N_PROTOCOL=https
      - NODE_ENV=production
      - WEBHOOK_URL=https://${SUBDOMAIN}.${DOMAIN_NAME}/
      - GENERIC_TIMEZONE=Europe/Athens
      - TZ=Europe/Athens
      - QUEUE_HEALTH_CHECK_ACTIVE=true
      - EXECUTIONS_DATA_SAVE_ON_ERROR=all
      - EXECUTIONS_DATA_SAVE_ON_SUCCESS=none
      - EXECUTIONS_DATA_SAVE_ON_PROGRESS=true
      - EXECUTIONS_DATA_SAVE_MANUAL_EXECUTIONS=true
      - EXECUTIONS_DATA_PRUNE=true
      - EXECUTIONS_DATA_MAX_AGE=1
      - DB_SQLITE_VACUUM_ON_STARTUP=true
      - NODE_FUNCTION_ALLOW_EXTERNAL=mout,iso-3166-1-alpha-2,mysql,@hubspot/api-client,axios,sanitize-html,dom-parser,bottleneck,node-html-parser,html-table-to-json,cheerio
      - NODE_FUNCTION_ALLOW_INTERNAL=axios,sanitize-html,=mout,iso-3166-1-alpha-2,mysql,@hubspot/api-client,axios,sanitize-html,dom-parser,bottleneck,node-html-parser,html-table-to-json,cheerio
    volumes:
      - n8n_2024_data:/home/node/.n8n
    networks:
      - traefik_proxy
networks:
  traefik_proxy:
    external: true

volumes:
  n8n_2024_data:
    external: true

Share the output returned by the last node

Information on your n8n setup

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

Hi @yousername! Always a good question. We’ve had a few answers to it shared before. I’ll drop the links below:

Happy building!

thank you @Ludwig!

How to achieve it using docker-compose?

Also I have added these 2 below to the environment configuration :melting_face: :upside_down_face: :joy: but no luck. Any suggestion?

      - NODE_FUNCTION_ALLOW_EXTERNAL=mout,iso-3166-1-alpha-2,mysql,@hubspot/api-client,axios,sanitize-html,dom-parser,bottleneck,node-html-parser,html-table-to-json,cheerio
      - NODE_FUNCTION_ALLOW_INTERNAL=axios,sanitize-html,=mout,iso-3166-1-alpha-2,mysql,@hubspot/api-client,axios,sanitize-html,dom-parser,bottleneck,node-html-parser,html-table-to-json,cheerio

Fair question.

You can use the wildcard value for the external npm modules you want to allow instead of specifying the individual name. I’d also recommend checking to make sure you’re installing them globally, and rebuilding your docker container.

Thanks for your support @Ludwig!

if my installation folder that contains docker-compose.yml is in this path ~/n8n_new/, where is external node_modules relative to that path?

Is external node_modules means that it’s located inside this folder ~/n8n_new/?

I have tried rebuild but with NODE_FUNCTION_ALLOW_EXTERNAL set to wildcard and have node_modules inside the installation folder, unfortunately it’s still doesn’t work :frowning:

docker-compose down
docker-compose up -d

actually problem not solved yet.

I can use Cheerio module but not mysql.

I have tried this command below, problem persists.

docker-compose up --build -d 

As seen in the package.json generated by docker-compose, it contains mysql and the rest of the stuff.

I’d like all modules in this to be accessible by n8n

      - NODE_FUNCTION_ALLOW_EXTERNAL=mout,iso-3166-1-alpha-2,mysql,@hubspot/api-client,axios,sanitize-html,dom-parser,bottleneck,node-html-parser,html-table-to-json,cheerio

Fortunately Cherio package is already included in n8n by default, so it’s not a problem.

But I have issues adding different packages like sanitize-html.

Below is a new log, i desparately need this package sanitize-html.

Please help me guys :upside_down_face:

VMError: Cannot find module 'sanitize-html' at LegacyResolver.resolveFull (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver.js:126:9) at LegacyResolver.resolveFull (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver.js:316:16) at LegacyResolver.resolveFull (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver-compat.js:147:17) at LegacyResolver.resolve (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver.js:121:15) at resolve (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/nodevm.js:317:21) at VM2 Wrapper.apply (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/bridge.js:485:11) at requireImpl (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/setup-node-sandbox.js:90:19) at require (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/setup-node-sandbox.js:171:10) at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code:2:1 at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code:6:2

Problem solved by accessing the container as a root.

Then perform installation with -g for global

docker exec -it -u root n8n_2024 /bin/sh

npm install -g sanitize-html

proof of work

1 Like

for my future self and anyone.

When rebuilding the image using this command below, all modules are gone.

`docker-compose up --build -d`

solution is to execute the following .sh file, it will install additional modules

#sh file

#stop current docker
docker-compose stop;

docker-compose up --build -d;

#install your modules in the docker as a global
echo "installing external  modules"

#version 1 exec command
docker exec -it -u root n8n_2024 /bin/sh -c "npm install -g mout iso-3166-1-alpha-2 mysql @hubspot/api-client axios sanitize-html dom-parser bottleneck node-html-parser html-table-to-json cheerio"

#or use this alternative version 2 exec command
docker-compose exec -u root n8n_2024 npm install -g mout iso-3166-1-alpha-2 mysql @hubspot/api-client axios sanitize-html dom-parser bottleneck node-html-parser html-table-to-json cheerio

#both commands above achieve similar thing. 2nd exec command is more elegant to me.

echo "installation done"

echo "N8N ready for use"
~

3 Likes

Hi there! Glad to hear you are unblocked. The dependencies need to be installed globally inside the container, which you are getting around to by accessing the container and doing it manually there.

Also feel free to check out Docker’s documentation for additional details around some of the flags you’re using when running your compose commands.

Thanks!

If I do it using Dockerfile, yes the server is up but it isn’t accessible, meaning error.

Dockerfile

FROM n8nio/n8n
USER root
RUN npm install -g mout iso-3166-1-alpha-2 mysql @hubspot/api-client axios sanitize-html dom-parser bottleneck node-html-parser html-table-to-json cheerio
USER node

docker-compose.yml

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

Error

n8n_test    | su-exec: setgroups: Operation not permitted

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