Import chatgpt error .. ERROR: 'import' and 'export' may only appear at the top level [line 1]

I installed this chatgpt-api (GitHub - transitive-bullshit/chatgpt-api: Node.js client for the official ChatGPT API. 🔥) node module and configured it correctly

This is an ESM module and I am trying into import into a code block

import { ChatGPTAPI } from 'chatgpt'

It throws the error:

ERROR: 'import' and 'export' may only appear at the top level [line 1]

I tried other variation of dynamic import mentioned here
… but it still doesn’t work. (dynamic import not supported)

Any suggestions?

Hi @Santosh_Srinivas, in general I suggest avoid using custom modules whenever possible. Such workflows cannot typically be transferred across different n8n instances unless they both run a custom environment making such a module available. So when trying to use an API endpoint not available in the n8n OpenAI node you might want to consider using the HTTP Request node instead.

That said, if you do want to use the Code node with custom npm modules you could import them using require like so:

require might not be supported by all npm packages though and I am unfortunately not familiar with the specific package you have linked.

1 Like

Thank you. Actually this module is weird, its an ESM module and the imports dont seem to work.
Can you pls advise if there is a way to do this?

Hi @Santosh_Srinivas, I am afraid not as I am not familiar with this module. So I reckon your best bet would be to simply use the HTTP Request node if you want to interact with the OpenAI API outside of the OpenAI node.

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