Problem loading "stringify-object" module after update to v0.135.0

I updated my n8n instance (Docker, PostgresDB) to v0.135.0

After that, one of my Function nodes, that was using an external library, errored.

Concerned statement:

const stringifyObject = require('stringify-object');

Error:

Stack
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /usr/local/lib/node_modules/stringify-object/index.js
require() of ES modules is not supported.
require() of /usr/local/lib/node_modules/stringify-object/index.js from /usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /usr/local/lib/node_modules/stringify-object/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at Object.require (internal/modules/cjs/helpers.js:88:18)
    at Object.EXTENSIONS.<computed> [as .js] (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/sandbox.js:57:48)
    at _require (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/sandbox.js:406:24)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:1:122
    at Object.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:278:2)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1167:29)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:81:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:500:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:430:62
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

So, i immediately rolled back to v0.134.0 and it’s back to working normally.

Hey @shrey-42!

Thank you for letting us know. Is this an external package that you’re using in your installation? From the error message, it seems like you will have to use import stringifyObject from 'stringify-object'. Did you try that?

Hey @harshil1712 .

Yes.


Yes, and that doesn’t work either. Error:

Stack
/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:1
(function (exports, require, module, __filename, __dirname) { module.exports = async function() {import stringifyObject from 'stringify-object'; 
                                                                                                 ^^^^^^

SyntaxError: Cannot use import statement outside a module
    at new Script (vm.js:101:7)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1153:13)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:81:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:492:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:424:62
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

hey @shrey-42

I saw this topic and was wondering if this library got removed.

Then I went back to a previous version of n8n and realized this library was not part of n8n at all.

Searching through the library repo, I realized a new version has just been released that is probably the reason your n8n is not loading it anymore: it has been converted to ESM.

How are you installing it to your system? Perhaps using version 3.3.0 instead of the latest one should solve your issue.

2 Likes

Hey @krynble,

It’s set up via a Dockerfile.

Just tried it, and it does work with the 3.3.0 (non-ESM) version.

Although, what would be a more permanent solution for this, and other ESM packages?

The problem is that the VM will run your code as CJS and the library is now ESM.

There is some more information about why it won’t work here: Node Modules at War: Why CommonJS and ES Modules Can’t Get Along | by Dan Fabulich | Code Red