Code node, cannot use external npm module

Describe the problem/error/question

Hi. I’m using n8n self hosted on a Docker environment, the n8n version is 1.25.1
I have noticed that from about two weeks my Function Node has ceased to function properly.
I’m using the Function Node to make some tests using the chai library.

At the start of the code, i require the library:
“var assert = require(‘chai’).assert;”
But i got the following error:
“Error [ERR_REQUIRE_ESM]: require() of ES Module /usr/local/lib/node_modules/chai/chai.js from /usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver-compat.js not supported.
Instead change the require of chai.js in /usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver-compat.js to a dynamic import() which is available in all CommonJS modules.”

I’m using the chai library found here → GitHub - chaijs/chai: BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.
I’ve searched and tried any sort of things/workarounds to make it work, but nothing seems to function. Unfortunately i cannot say exactly when it ceased to function.

The only configuration i have is the ENV variable:

chai version is 5.0.3
npm version is 9.5.1

Can you help me? Is there a way on how to use an external npm library inside a Function node? Am i doing somthing wrong?

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

Hi, sorry i tought i put some data in my previous message, here are all the info:

  • n8n version: 1.25.1
  • Database: default
  • n8n EXECUTIONS_PROCESS setting: default Also i have seen that its deprecated and instead now is used EXECUTIONS_MODE. This setting i have let it default since i did not rewrite it.
  • Running n8n via: Docker I have a Docker Image configured to self host on a custom domain.
  • Operating system: The instance of n8n runs on a Machine that is Ubuntu 20.04.6, but is Dockerized so is isolated.

Other configuration on the DockerImage regarding the ENV variables:

This is the Function Node:

Hi there @Sengerio :wave: Thanks for the additional information - Maybe @Jon or @marcus could help with this one :bowing_man:

Hey @Sengerio,

It looks like the error has pretty much nailed it… You can’t use require() and instead need to use import

Error [ERR_REQUIRE_ESM]: require() of ES Module /usr/local/lib/node_modules/chai/chai.js from /usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver-compat.js not supported.
Instead change the require of chai.js in /usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver-compat.js to a dynamic import() which is available in all CommonJS modules.

I am not familiar with Chai but looking at the docs something like import { assert } from 'chai'; might be what you are after.

Hi @Jon. Sorry i should i have told in the message sooner. I’ve already tried that solution, but it gives me error. Here, let me show you.

The error is:

module.exports = async function() {import { assert } from ‘chai’;
SyntaxError: ‘import’ and ‘export’ may only appear at the top level
at makeNiceSyntaxError (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/transformer.js:41:16)
at transformer (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/transformer.js:84:8)
at (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/nodevm.js:485:17)
at JavaScriptSandbox.runCodeAllItems (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/JavaScriptSandbox.js:51:45)
at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Code.node.js:116:40)
at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:698:42)
at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:656:68

It’s as the function node could not allow the implementation of the import function. Here is the updated node:

Ahh yeah I forgot about that, There are some packages that may not work at all it could be that Chai is one of them. If I get a chance I will build a docker image with it and see if I can find a way to get it working.


I encountered the same problem with another package (Can't use 'require package' in custom function after updating to 1.20.0 - #3 by CristianG).
I believe it has to do with how these new packages are developed.
I am not a skilled Node developer, so I cannot identify the exact issue.
In my case, the solution was to use an older version of the npm package.
Try using an older version of the chai package, found here: chai - npm

@Jon, I guess we will need to figure out how to import these newer packages that are using ESM.
I think this is a starting point: javascript - Error [ERR_REQUIRE_ESM]: require() of ES Module not supported - Stack Overflow, but I couldn’t fully understand the leading cause of the error, other than the fact that the developers used ESM in their packages.
As more developers are expected to start using it soon, we will need to find a way to import these packages into a Code node.

Goodness. So n8n can’t handle nodes that use esm modules? Yikes.

@tempire Would need to do some digging but that could be the case because of the javascript sandbox, While it may not be directly useable in the code node though you could still build a custom node that uses it or use execute command to run a script that is using it.