How to use 'net' Module in Code Node

Describe the problem/error/question

In the Code node, const n = require(‘net’); throws an error, even though ‘net’ should be an allowed module by using -e NODE_FUNCTION_ALLOW_EXTERNAL=net,ssh2,node,node:net (by using this config, ‘ssh2’ is available to the Code node, but ‘net’ and ‘node:net’ are not).

The Execute Command node is able to run nodejs and use net: nodejs -e “const n = require(‘net’); console.log(n);” outputs the expected contents of the net module. So net seems to be installed/available to node just not n8n.

But the Code node throws the error when trying do the same thing.

What is the error message (if any)?

ERROR: Cannot find module ‘net’ [line 1]

Please share your workflow

Information on your n8n setup

  • n8n version: 1.16
  • Database (default: SQLite): Postgres
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Ubuntu

Hi @jzrts, net is a built-in module provided by Node.js: Net | Node.js v18.18.2 Documentation

As such you’d need to allow its use through the NODE_FUNCTION_ALLOW_BUILTIN environment variable. For example through a docker run command like this

docker run -it --rm --name n8n -p 5678:5678 -v ~/.n8n:/home/node/.n8n -e NODE_FUNCTION_ALLOW_BUILTIN=net n8nio/n8n:1.18.0

Afterwards you can use it in a workflow like so for example:

Result:

Hope this helps :slight_smile:

Thanks for the clarification! I didn’t realize it’s builtin.

1 Like

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