How to use nodejs require module

Describe the problem/error/question

When using a Code node, checking the current __dirname shows that it’s /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes.

However, when checking console.log('resolve-path: ', path.resolve()), it shows resolve-path: /home/node.

Although the file /home/node/modules/test/TestNode.node.js exists, I get an error saying it cannot be found when trying to require it.

The console.log('relativePath: ',fs.existsSync(path.resolve(relativePath,‘modules/test/TestNode.node.js’))) shows relativePath: true, meaning the file exists at the constructed path.

I’m using a local Docker setup.

Is there any way to import user code using require?

What is the error message (if any)?

Cannot find module ‘/home/node/modules/test/TestNode.node.js’ [line 16]

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version: : 1.77.3

  • Database (default: SQLite):: default

  • n8n EXECUTIONS_PROCESS setting (default: own, main):

  • Running n8n via (Docker, npm, n8n cloud, desktop app): : docker

  • Dockerfile

FROM node:20-alpine AS builder

# pnpm 설치
RUN npm install -g pnpm

WORKDIR /app
COPY ./package*.json ./pnpm-lock.yaml* ./
RUN pnpm install --frozen-lockfile --prod

FROM n8nio/n8n:latest
ENV NODE_FUNCTION_ALLOW_EXTERNAL=*
ENV NODE_FUNCTION_ALLOW_BUILTIN=*
USER node

COPY --from=builder --chown=node:node /app/node_modules /usr/local/lib/node_modules

COPY --chown=node:node ./modules /home/node/modules

WORKDIR /home/node

ENTRYPOINT ["n8n"]
  • docker-compose.yml
services:
  n8n:
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    ports:
      - "5678:5678"
    environment:
      - N8N_HOST=localhost
      - N8N_PORT=5678
      - N8N_PROTOCOL=http
      - WEBHOOK_URL=http://localhost:5678/
      - GENERIC_TIMEZONE=Asia/Seoul
      - TZ=Asia/Seoul
      - N8N_BASIC_AUTH_ACTIVE=false
      - N8N_BLOCK_ENV_ACCESS_IN_NODE=false
      - NODE_FUNCTION_ALLOW_BUILTIN=*
      - NODE_FUNCTION_ALLOW_EXTERNAL=*
      - N8N_DIAGNOSTICS_ENABLED=true 
      - NODE_TLS_REJECT_UNAUTHORIZED=0
    volumes:
      - n8n_data:/home/node/.n8n
      - ./modules:/home/node/modules
      - ./n8n_data:/home/node/data
      - /etc/ssl/certs:/etc/ssl/certs
    env_file:
      - .env

volumes:
  n8n_data:
    external: true
  • Operating system:

Welcome to the community @ngionicc!

Tip for sharing information

Pasting your n8n workflow


Ensure to copy your n8n workflow and paste it in the code block, that is in between the pairs of triple backticks, which also could be achieved by clicking </> (preformatted text) in the editor and pasting in your workflow.

```
<your workflow>
```

That implies to any JSON output you would like to share with us.

Make sure that you have removed any sensitive information from your workflow and include dummy or pinned data with it!


Here’s an example of using external modules

const { createHmac } = require('crypto');
const moment         = require('moment');

const secret = 'abcdefg';
const hash = createHmac('sha256', secret)
               .update('I love cupcakes')
               .digest('hex');

const d = moment().subtract(10, 'days').calendar();

return { hash, d };

If you are self-hosting, you would list the modules you want to use with environmental variables as per Enable modules in Code node | n8n Docs.

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