Since 2.x: How to install external node module for use in Code node?

I used to work with the solution everybody uses who wants to install additional node_modules for use in the Code node: I installed them as glocal node modules in the docker container. I personally use a custom Dockerfile with lines like:

USER root
RUN npm install -g vcards-js
....

I also set export NODE_FUNCTION_ALLOW_EXTERNAL=* as explained in Enable modules in Code node | n8n Docs but since n8n 2.x it doesn’t work anymore. I only get something like:

Cannot find module 'vcards-js' Require stack: - /usr/local/lib/node_modules/n8n/node_modules/.pnpm/@n8n+task-runner@file+packages+@n8n+task-runner_@[email protected]_@opentelemetry_eb51b38615a039445701c88b088f88d0/node_modules/@n8n/task-runner/dist/js-task-runner/require-resolver.js - /usr/local/lib/node_modules/n8n/node_modules/.pnpm/@n8n+task-runner@file+packages+@n8n+task-runner_@[email protected]_@opentelemetry_eb51b38615a039445701c88b088f88d0/node_modules/@n8n/task-runner/dist/js-task-runner/js-task-runner.js - /usr/local/lib/node_modules/n8n/node_modules/.pnpm/@n8n+task-runner@file+packages+@n8n+task-runner_@[email protected]_@opentelemetry_eb51b38615a039445701c88b088f88d0/node_modules/@n8n/task-runner/dist/start.js

Does anybody know how to fix this? Is it maybe because of the newly introduced task runners mentioned on Enable modules in Code node | n8n Docs ?

But if so: I use an internal task runner. So how can I change the env vars for it?

@n8n: An update to the docs would be nice! (Or did I miss something?)

1 Like

hi, @nm5182 !

To fix this, you need to install the dependency inside the task runner environment rather than globally, either by declaring it in N8N_RUNNERS_JS_PACKAGES so the runner installs it automatically, or by bundling the dependency into a custom task runner or n8n image.

Thanks a lot but I couldn’t find any documentation about the env var you mentioned. And just for understanding: Am I supposed to put this into the environment key of my n8n service in my docker-compose file if I run runners internally?

@nm5182 ,

the best and currently supported approach is to use a custom Docker image and install the required dependencies locally, not globally.

That env variable does not exist anywhere in any n8n repository. Did you vibe-imagine it with AI?

I believe currently the only ‘stable’ way is making a custom docker image, extending the official task runner image. Inside the Dockerfile you would do npm install with the packages you need.

I’ve done this in my repo to build custom main n8n image (not task runner image), and it works.

The only ‘confusing’ part is that the docker image does not have apk, so you need to first add apk to add npm if I remember correctly.

Any other way of installing npm packages does not seem to work, like running npm install in ExecuteCommand node (no permissions, etc)

@Gkp1 Thanks a lot for your response. I also used to create my own n8n docker image. I always installed the additional node modules globally, but this doesn’t seem to work properly anymore. So I also tried to install my additional modules into /usr/local/lib/node_modules/n8n/ but I still get the error mentioned in my initial post. Could you give me some more details about how you manage to install external node modules and make them available for the Code node in n8n?

@nm5182

In n8n 2.x, you generally don’t “manage” external Node.js modules for the Code node the same way as before.

The Code node runs in an isolated environment, so modules installed globally (npm install -g) or manually copied into node_modules are not reliably available anymore.

The supported approaches are:

Use n8n Community Packages

If the module is packaged as a community node, install it via n8n’s community packages mechanism. This is the only officially supported way to make external functionality available inside n8n.

Keep the Code node self-contained

The Code node is intended for logic using built-in Node.js APIs and simple JS, not as a general-purpose runtime with arbitrary npm dependencies.

Move dependency-heavy logic outside n8n

For libraries that must be installed via npm, run them in:

A separate service

An external script

Or another container

…and call it from n8n (HTTP, Execute Command, etc.).

So in short: there is no supported way to “inject” arbitrary npm modules into the Code node anymore. If a dependency is required, it should either be a community package or live outside n8n.

Thanks for your response. Why is there official documentation for enabling external npm modules though? Enable modules in Code node | n8n Docs