Help needed with building custom Node

I need help with building my custom Node

Hello together,
for the first time I tried to build a custom Node. I’m not very familiar with Nodejs/NPM. AI helped a lot :wink:

My Goal is, to build a Trigger Node for the HomeAssistant integration, which uses Websockets - as without a trigger, the HomeAssistant Integration is nearly not useable.

While building I sticked to the official tutorial…
That’s my actual status:

  • I can build the Node without errors
  • I have created a link
  • I’ve added the link to the custom nodes Folder

You can find my code here: n8n-nodes-homeassistanttrigger

Without the custom Node, n8n starts without problems. When linking the custom Node, I’m getting the following error:

User settings loaded from: /home/MySpace/.n8n/config
Initializing n8n process
Error loading node "globals" from: "/home/MySpace/.n8n/custom/node_modules/n8n-nodes-homeassistanttrigger/node_modules/engine.io-client/build/cjs/globals.node.js" - require(...).globals is not a constructor
evalmachine.<anonymous>:1
new (require('/home/MySpace/.n8n/custom/node_modules/n8n-nodes-homeassistanttrigger/node_modules/engine.io-client/build/cjs/globals.node.js').globals)()
^

TypeError: require(...).globals is not a constructor
    at evalmachine.<anonymous>:1:1
    at Script.runInContext (node:vm:134:12)
    at loadClassInIsolation (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/ClassLoader.ts:9:16)
    at CustomDirectoryLoader.loadNodeFromFile (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/DirectoryLoader.ts:91:35)
    at CustomDirectoryLoader.loadAll (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/DirectoryLoader.ts:335:9)
    at LoadNodesAndCredentials.runDirectoryLoader (/usr/local/lib/node_modules/n8n/src/load-nodes-and-credentials.ts:270:3)
    at LoadNodesAndCredentials.loadNodesFromCustomDirectories (/usr/local/lib/node_modules/n8n/src/load-nodes-and-credentials.ts:181:4)
    at LoadNodesAndCredentials.init (/usr/local/lib/node_modules/n8n/src/load-nodes-and-credentials.ts:100:3)
    at Start.init (/usr/local/lib/node_modules/n8n/src/commands/base-command.ts:72:3)
    at Start.init (/usr/local/lib/node_modules/n8n/src/commands/start.ts:192:3)
Exiting due to an error.
Error: Exiting due to an error.
    at Start.exitWithCrash (/usr/local/lib/node_modules/n8n/src/commands/base-command.ts:151:23)
    at Start.catch (/usr/local/lib/node_modules/n8n/src/commands/start.ts:363:14)
    at Start._run (/usr/local/lib/node_modules/n8n/node_modules/@oclif/core/lib/command.js:306:29)
    at Config.runCommand (/usr/local/lib/node_modules/n8n/node_modules/@oclif/core/lib/config/config.js:424:25)
    at run (/usr/local/lib/node_modules/n8n/node_modules/@oclif/core/lib/main.js:94:16)
    at /usr/local/lib/node_modules/n8n/bin/n8n:71:2

require(...).globals is not a constructor
evalmachine.<anonymous>:1
new (require('/home/MySpace/.n8n/custom/node_modules/n8n-nodes-homeassistanttrigger/node_modules/engine.io-client/build/cjs/globals.node.js').globals)()
^

TypeError: require(...).globals is not a constructor
    at evalmachine.<anonymous>:1:1
    at Script.runInContext (node:vm:134:12)
    at loadClassInIsolation (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/ClassLoader.ts:9:16)
    at CustomDirectoryLoader.loadNodeFromFile (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/DirectoryLoader.ts:91:35)
    at CustomDirectoryLoader.loadAll (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/DirectoryLoader.ts:335:9)
    at LoadNodesAndCredentials.runDirectoryLoader (/usr/local/lib/node_modules/n8n/src/load-nodes-and-credentials.ts:270:3)
    at LoadNodesAndCredentials.loadNodesFromCustomDirectories (/usr/local/lib/node_modules/n8n/src/load-nodes-and-credentials.ts:181:4)
    at LoadNodesAndCredentials.init (/usr/local/lib/node_modules/n8n/src/load-nodes-and-credentials.ts:100:3)
    at Start.init (/usr/local/lib/node_modules/n8n/src/commands/base-command.ts:72:3)
    at Start.init (/usr/local/lib/node_modules/n8n/src/commands/start.ts:192:3)

I think, It has something to do with teh websocket lib, which I use. I’ve looked into other Nodes, which use websockets and copied from there.

Please help me!

Information on your n8n setup

  • n8n version: npm 9.2.0
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • Operating system: ubuntu 24.04.1

Hello @ptC7H12! :wave:

As you said, it looks like the problem is with the socket.io-client dependency that you’re importing and its dependency on engine.io-client. There’s an expectation of it being a class that we can instantiate.

Have you got an example of a custom node with using the socket.io library that works? And have you tried using another websocket library like ws?

Hi and thank you for your response!

I took these repos as template:
n8n/packages/nodes-base/nodes/HomeAssistant at ha-trigger · pemontto/n8n

n8n-nodes-websockets/nodes/websockets/WebsocketsNode.node.ts at master · zampierid4p/n8n-nodes-websockets

From the 2nd I took the librarys and from the first the process I need for HomeAssistant.
I do not havy any experience in building Nodes, so I’m reliant on help here.

As there is no Homeassistant trigger, but which is really essential, I tried to build one.

I appreciate every help

Thanks for the links.

Did the 2nd node work for you?
It uses the socket.io library too and it would be good to check that this has no problems with the import so we can understand if it’s a problem with that library or not.

Can you try to get the WebsocketsNode custom node working in your n8n instance?

I installed it, but I couldn’t get it to work as I only have HomeAssistant as a websocket endpoint - there was a loading circle while connecting, but did not get any data.

Maybe because HomeAssistant need different steps within the connecting process:
HomeAssistant: WebSocket API | Home Assistant Developer Docs

  1. Authentication phase
  2. Command phase
  3. Subscribe to events or Subscribe to trigger
  4. do other action

The websocket client from the link does only:

  1. Authentication phase
  2. listen for configured event

It doesn’t have the support for subscribing events - maybe this is the problem!

For answering your question as good as I can.

  • I can install the custom Node
  • I can configure it
  • I can start it
  • Then I don’t see anywhere, that something happens

Does this help?
Anything else I can check?

Can you guide me, what I can check more?

I’m not super familiar with Home Assistant I’m afraid.
Can you test it with a local websocket server instead of with Home Assistant?
Something like this perhaps: Set up a WebSocket server in Node.js | Postman Blog

I wonder if we can test it by just checking for basic messages from your own WS server.

For the Home Assistant, I’d assume you’d need to respond to the authentication phase and once you’re authenticated, you could send JSON payloads with the expected commands.