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.”
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:
ENV NODE_FUNCTION_ALLOW_EXTERNAL=chai
ENV EXECUTIONS_DATA_PRUNE=true
ENV EXECUTIONS_DATA_MAX_AGE=336
ENV EXECUTIONS_DATA_PRUNE_MAX_COUNT=20000
ENV N8N_PUSH_BACKEND=sse
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:
/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code:1
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 NodeVM.run (/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.
@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.