Referencing npm "clean-html" inside n8n

ash-keywords
HOBBY

18 days ago

I need to use this nmp library inside n8n by using require("clean-html") in my code. Can you please advise how I can do that? as far as Railway is concerned?

I configured the env variable for n8n as explained here:
https://docs.n8n.io/code/code-node/#javascript
https://docs.n8n.io/hosting/configuration/configuration-examples/modules-in-code-node/

My install is setup using https://hub.docker.com/r/n8nio/n8n (the popular n8n template on railway).

I tried adding a pre-deplyment comment "npm install clean-html" in my deployment steps but that didn't seem to do anything.. Your step by step guidance is appreciated.

If I absolutely need to redeploy n8n using a custom docker file, then please provide details on how to do that without losing data in the current install.

THANK YOU!

Solved$10 Bounty

7 Replies

18 days ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open brody 18 days ago


I didn't quite understand what you're trying to do, so maybe it's not even necessary to clean html. However, you do need a custom Dockerfile to install extra npm packages for n8n on Railway. Deploying this way won’t lose your data, and you’ll be able to use clean-html in your workflows.

First you’ll need to make sure it’s actually installed in the same environment where n8n runs. The “pre-deployment command” approach doesn’t work with the official n8n Docker image on Railway, because each deploy starts from a fresh image and doesn’t persist your npm install.

To create a custom Dockerfile, just start with the official n8n image, but add a line to install clean-html:

FROM n8nio/n8n:latest
USER root
RUN npm install --prefix /usr/local/lib/node_modules clean-html
USER node

To deploy it: go to your service settings and select “Deploy from GitHub repo.”

  • Put your Dockerfile in the repo, commit, and push.

  • Finally, set up n8n to allow external modules:

    • In Railway’s environment variables, set:

      N8N_ENABLE_EXTERNAL_MODULES=true
      

      I had to do this for a configuration required to handle some advanced encryption for WhatsApp API. Took me like 3 days to fix lol.


ash-keywords
HOBBY

18 days ago

Thanks for the quick response. To answer your questions, I need a more advanced HTML parsing library to trim unnecessary parts from a fairly large HTML document before sending it to the LLM.

Regarding editing the original Dockerfile, could you please provide detailed steps to ensure I'm editing the correct file and deploying it properly? I'm concerned about potential data loss if I make a mistake.

As I mentioned, this is the template I originally installed: https://railway.com/deploy/r2SNX_, which uses this Docker image: https://hub.docker.com/r/n8nio/n8n.
Under
"Primary," I went to Settings -> Source and disconnected the repo. Now I see options to connect a GitHub repo or connect an image.

Your instructions are clear except for how to obtain the original Dockerfile safely to confirm it's the right one without risking data loss. (Apologies, but I am new to docker, so I'm not 100% sure I know where to extract the original file from this page: https://hub.docker.com/r/n8nio/n8n)

Also, to confirm: will these changes affect the current connections with Postgres or Redis? Will any environment variables be changed or modified when introducing a new source?

Thank you for your help!


You don’t need the original Dockerfile—just make a new one in your repo like this:

FROM n8nio/n8n:latest
USER root
RUN npm install --prefix /usr/local/lib/node_modules clean-html
USER node

Push this to GitHub, connect your Railway project to the repo, and deploy. Your Postgres/Redis settings and data won’t be touched so no environment variable changes. This just adds the npm package.


testuser123

You don’t need the original Dockerfile—just make a new one in your repo like this:FROM n8nio/n8n:latest USER root RUN npm install --prefix /usr/local/lib/node_modules clean-html USER nodePush this to GitHub, connect your Railway project to the repo, and deploy. Your Postgres/Redis settings and data won’t be touched so no environment variable changes. This just adds the npm package.

ash-keywords
HOBBY

18 days ago

Thank you! The build was successful but for some reason the require still can't find the library.

I switched to "sanitize-html" since it seems like a more established package.

The contents of my Dockerfile is:

FROM n8nio/n8n:latest

USER root

RUN npm install --prefix /usr/local/lib/node_modules sanitize-html

USER node

The build and deployment seems to be a success.

regarding env variables. I have the following set:
N8N_ENABLE_EXTERNAL_MODULES = true

NODE_FUNCTION_ALLOW_BUILTIN = * (I tried both asterisk and sanitize-html)

NODE_FUNCTION_ALLOW_EXTERNAL = *

and I still seem to get the "cannot find module" error.

Any thoughts what else I could be missing?

Thanks again for your help.


ash-keywords

Thank you! The build was successful but for some reason the require still can't find the library.I switched to "sanitize-html" since it seems like a more established package.The contents of my Dockerfile is:FROM n8nio/n8n:latestUSER rootRUN npm install --prefix /usr/local/lib/node_modules sanitize-htmlUSER nodeThe build and deployment seems to be a success.regarding env variables. I have the following set:N8N_ENABLE_EXTERNAL_MODULES = trueNODE_FUNCTION_ALLOW_BUILTIN = * (I tried both asterisk and sanitize-html)NODE_FUNCTION_ALLOW_EXTERNAL = *and I still seem to get the "cannot find module" error.Any thoughts what else I could be missing?Thanks again for your help.

Dude, I just realized you might not need that.

Why don't you just use the Markdown node? It'll clean the html you have and send it to the LLM neatly. That's what I do and it works great. Lots of options to customize too. Wanna give it a shot?


testuser123

Dude, I just realized you might not need that.Why don't you just use the Markdown node? It'll clean the html you have and send it to the LLM neatly. That's what I do and it works great. Lots of options to customize too. Wanna give it a shot?

ash-keywords
HOBBY

18 days ago

Thanks. I tried that already but there is some nesting in the HTML that gets lost when translated to markdown. Plus, I need to figure out the external lib anyway since I'm sure there will be other packages that I'll likely need down the road. I feel like I'm so close to figuring it out! So I don't want to give up inches from the finish line. hah!


ash-keywords

Thanks. I tried that already but there is some nesting in the HTML that gets lost when translated to markdown. Plus, I need to figure out the external lib anyway since I'm sure there will be other packages that I'll likely need down the road. I feel like I'm so close to figuring it out! So I don't want to give up inches from the finish line. hah!

ash-keywords
HOBBY

17 days ago

Fixed!! Turns out you need to set NODE_FUNCTION_ALLOW_EXTERNAL on BOTH Primary and Worker n8n deployments. I've been focused on Primary that I didn't even consider making any changes elsewhere. Thanks for the help! I


Status changed to Solved brody 17 days ago