N8n fail to run a code with a custom package

I’m using a custom Dockerfile to install some extra packages.

FROM n8nio/n8n

USER root

RUN npm install -g hyperformula
RUN npm install -g lodash
RUN npm install -g moment
RUN npm install -g moment-timezone
RUN npm install -g moment-duration-format
RUN npm install -g axios

USER node

And using this Dockerfile on my Docker Compose file, enabling the required package using the environment variables.

n8n:
    # image: docker.n8n.io/n8nio/n8n
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    ports:
      - "127.0.0.1:5678:5678"
    environment:
      - N8N_HOST=${SUBDOMAIN}.${DOMAIN_NAME}
      - N8N_PORT=5678
      - N8N_PROTOCOL=https
      - NODE_ENV=production
      - WEBHOOK_URL=https://${SUBDOMAIN}.${DOMAIN_NAME}/
      - GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
      - NODE_FUNCTION_ALLOW_BUILTIN="*"
      - NODE_FUNCTION_ALLOW_EXTERNAL="hyperformula,moment,lodash"
      - N8N_REINSTALL_MISSING_PACKAGES=true
      - N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true
    volumes:
      - n8n_data:/home/node/.n8n
      - ./local-files:/files

Then while trying to run the Code I’m getting:

{
  "errorMessage": "Cannot find module 'hyperformula' [line 1]",
  "errorDescription": "VMError",
  "errorDetails": {},
  "n8nDetails": {
    "nodeName": "Code",
    "nodeType": "n8n-nodes-base.code",
    "nodeVersion": 2,
    "n8nVersion": "1.91.3 (Self Hosted)",
    "binaryDataMode": "default",
    "stackTrace": [
      "VMError: Cannot find module 'hyperformula'",
      "    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:490: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:1:121",
      "    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code:39:2"
    ]
  }
}

The code:

Please share your workflow

did you try these solutions ? How to install node packages, where's the node_modules? - #2 by Ludwig

2 Likes

Hi @anthony31 . Following the solution found in the Luwig post, I did the following change:

Instead of doing this:
NODE_FUNCTION_ALLOW_EXTERNAL=*
I have replaced this:
NODE_FUNCTION_ALLOW_EXTERNAL="hyperformula,moment,lodash"
By this:
NODE_FUNCTION_ALLOW_EXTERNAL=hyperformula,moment,lodash

Removing the double quotes in the DockerCompose file solved my issue.

I dont think you’re supposed to install the packages globally. They need to be installed into the local node_modules on the n8n instance for this to work.

Your dockerfile should look more like this:

FROM n8nio/n8n:latest

# Switch to root user to have permissions to install packages
USER root

# Install your npm package inside n8n folder
RUN cd ~/.n8n/nodes && npm install moment

# Switch back to the default 'node' user for security
USER node

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