Unable to load external scoped (@) module/package

Issue

Function code fails when attempting to load an external scoped module. Non-scoped external modules (such as moment) are loading fine. Not sure if this issue is related to a scoped namescape (the VM2 docs seem to indicate scoped packages/modules are allowed), but I’ve tried several so far and none of them have worked. Anyone else run into this?

Environment:

npm install -g \
	@turf/turf@^6.5.0 \
	moment@^2.14.1

NODE_FUNCTION_ALLOW_BUILTIN=*
NODE_FUNCTION_ALLOW_EXTERNAL=*

Function:

const moment = require('moment');
const turf = require('@turf/turf');

let test = [];
test.push({
  json: {test: moment().format()}
});

return test;

What is the error message?

ERROR: Cannot find module '@turf/turf'

Stack
VMError: Cannot find module '@turf/turf'
    at _require (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/sandbox.js:377:25)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function:2:14
    at Object.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function:10:2)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1233:29)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function/Function.node.js:81:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:524:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:447:62
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

Workflow

Information on your n8n setup

  • n8n version: 0.152.0
  • Database you’re using (default: SQLite): MySQL 5.7
  • Running n8n with the execution process [own(default), main]: queue
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Docker

Hi @derekseymour, have you installed your modules in the n8n/node_modules folder? This is where external modules would be sourced from as per our docs.

Just to be complete this is also raised here: Unable to load external scoped (@) module/package · Issue #2559 · n8n-io/n8n · GitHub

1 Like

Thanks @MutedJam Your question pointed me in the right direction.

While I had tried installing under both the n8n/node_modules and the global node_modules folders, what hit me was that I was only checking the worker container and not the main n8n container (since I’m running in queue mode).

My worker container is a custom-built Ubuntu image which is where I was doing my npm installs. I had assumed that was the only place the modules would be needed since that’s where the execution is happening, but apparently they needed to be installed within the main container (running the base “n8nio/n8n” Docker image) as well.

Solution: Make sure to install external npm modules in both the main and worker containers.

2 Likes