Error when using CLI to import workflows

Describe the problem/error/question

When I use the CLI to import all or a workflow, I get this error

What is the error message (if any)?

~ $ n8n import:workflow --input=/files/n8n-workflows/CpDs6GApbWob6kud.json
User settings loaded from: /home/node/.n8n/config
2024-07-03T22:56:52.654Z | debug    | Lazy Loading credentials and nodes from n8n-nodes-base "{\n  credentials: 356,\n  nodes: 453,\n  file: 'LoggerProxy.js',\n  function: 'exports.debug'\n}"
2024-07-03T22:56:52.669Z | debug    | Lazy Loading credentials and nodes from @n8n/n8n-nodes-langchain "{\n  credentials: 15,\n  nodes: 74,\n  file: 'LoggerProxy.js',\n  function: 'exports.debug'\n}"
2024-07-03T22:56:52.810Z | debug    | [Concurrency Control] Service disabled "{ file: 'concurrency-control.service.js', function: 'log' }"
2024-07-03T22:56:52.892Z | error    | An error occurred while importing workflows. See log messages for details. "{ file: 'workflow.js', function: 'catch' }"
2024-07-03T22:56:52.893Z | error    | workflows.map is not a function "{ file: 'workflow.js', function: 'catch' }"

~ $ n8n import:workflow --separate --input=/files/n8n-workflows/
User settings loaded from: /home/node/.n8n/config
2024-07-03T22:59:18.830Z | debug    | Lazy Loading credentials and nodes from n8n-nodes-base "{\n  credentials: 356,\n  nodes: 453,\n  file: 'LoggerProxy.js',\n  function: 'exports.debug'\n}"
2024-07-03T22:59:18.844Z | debug    | Lazy Loading credentials and nodes from @n8n/n8n-nodes-langchain "{\n  credentials: 15,\n  nodes: 74,\n  file: 'LoggerProxy.js',\n  function: 'exports.debug'\n}"
2024-07-03T22:59:18.976Z | debug    | [Concurrency Control] Service disabled "{ file: 'concurrency-control.service.js', function: 'log' }"
2024-07-03T22:59:19.127Z | info     | Importing 151 workflows... "{ file: 'workflow.js', function: 'run' }"
2024-07-03T22:59:19.378Z | info     | Deactivating workflow "Text To Speech". Remember to activate later. "{ file: 'import.service.js' }"
2024-07-03T22:59:19.394Z | error    | An error occurred while importing workflows. See log messages for details. "{ file: 'workflow.js', function: 'catch' }"
2024-07-03T22:59:19.395Z | error    | duplicate key value violates unique constraint "idx_812eb05f7451ca757fb98444ce" "{ file: 'workflow.js', function: 'catch' }"

The Credential imports works:

~ $ n8n import:credentials --separate --input=/files/n8n-credentials/
User settings loaded from: /home/node/.n8n/config
2024-07-03T22:55:32.672Z | debug    | Lazy Loading credentials and nodes from n8n-nodes-base "{\n  credentials: 356,\n  nodes: 453,\n  file: 'LoggerProxy.js',\n  function: 'exports.debug'\n}"
2024-07-03T22:55:32.690Z | debug    | Lazy Loading credentials and nodes from @n8n/n8n-nodes-langchain "{\n  credentials: 15,\n  nodes: 74,\n  file: 'LoggerProxy.js',\n  function: 'exports.debug'\n}"
2024-07-03T22:55:32.846Z | debug    | [Concurrency Control] Service disabled "{ file: 'concurrency-control.service.js', function: 'log' }"
2024-07-03T22:55:33.077Z | info     | Successfully imported 26 credentials. "{ file: 'credentials.js', function: 'reportSuccess' }"

Information on your n8n setup

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

Hi @MarioGuimaraes, thank you for reaching out and being a part of the community!

In what directory are you running your commands?

You might encounter that error if you’re running them outside of your configuration folder.

Hello, @Ludwig
Thanks for your answer

When I’m executing in /root/.n8n or in another folder, the same error happening

~/.n8n # ls -lah
total 32K    
drwxr-xr-x    5 root     root        4.0K Jul  4 22:42 .
drwx------    1 root     root        4.0K Jul  4 22:56 ..
drwxr-xr-x    2 root     root        4.0K Jul  4 22:42 binaryData
-rw-r--r--    1 root     root          54 Jul  4 22:42 config
drwxr-xr-x    2 root     root        4.0K Jul  4 22:42 git
-rw-r--r--    1 root     root        2.8K Jul  4 22:43 n8nEventLog.log
drwxr-xr-x    2 root     root        4.0K Jul  4 22:42 ssh
~/.n8n # n8n import:workflow --input=/files/n8n-workflows/1MPka8hRy25atJdb.json
User settings loaded from: /root/.n8n/config
An error occurred while importing workflows. See log messages for details.
workflows.map is not a function
~/.n8n # 

in root:

~ # pwd
/root
~ # n8n import:workflow --input=/files/n8n-workflows/1MPka8hRy25atJdb.json
User settings loaded from: /root/.n8n/config
An error occurred while importing workflows. See log messages for details.
workflows.map is not a function

Are you able to manually import that workflow into your instance? By clicking the “Import Workflow from File” in the triple dot menu at the top right when viewing a workflow.

This implies that CpDs6GApbWob6kud.json is a single workflow, and not an array. --input without a --separate expects a single JSON file with an array of all the workflows. The second command looks correct though. and we should also definitely detect and handle this mistake better.

The second command failed because the importing data has tags that already exist in your database, and while we do upserts for workflows and tags, looks like we missed some scenario.

I don’t know what’s going on, but now it works

@MarioGuimaraes did you do a full export again?

From reading the code my guess would be that you had multiple workflows in that directory and somehow they ended up having tags that had the same name, but different IDs. That would lead the current import code to try to create multiple tags with the same name which would violate the constraint we have in the db. That’s one way to explain the duplicate key value violates unique constraint.

But I’m happy that it works now :heart: