JS Code Node and require - error: cannot find module

Describe the problem/error/question

Cannot use common JS require. I’m getting error: Cannot find module ‘./fcts/myFct’
I have reproduced this in a very simple Code Node attached below.

I have tried:

  1. const myFct = require(‘./fcts/myFct’);
    with fcts a sub-directory of n8n

  2. const myFct = require(‘myDir/myFct’);
    with myDir a sub-directory in n8n/node_modules and myFct.js copied to that dir.

And I have the following env vars defined before starting n8n:
set N8N_RUNNERS_ENABLED=true
set N8N_NODE_FUNCTION_ALLOW_EXTERNAL=*
set N8N_NODE_FUNCTION_ALLOW_BUILTIN=*

And I have tested the equivalent code with a simple console Node.js program so I know there is no syntax issue with the require and in myFct.js

What am I missing?

Thanks for your help

What is the error message (if any)?

Cannot find module ‘./fcts/myFct’

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

{
“nodes”: [
{
“parameters”: {
“jsCode”: “// does not work\nconst myFct = require(‘./fcts/myFct’);\n\n// None of these are defined - so can’t find current dir\n//console.log("dirname from process: "+process.cwd());\n//console.log("filename: "+__filename);\n//console.log("dirname: "+__dirname);\n\n// From node_modules\n//const myFct = require(‘myDir/myFct’);\n//console.log(myFct.test());\n\n// Ref: https://docs.n8n.io/code/builtin/current-node-input/\n// Loop over input items and add a new field called ‘myNewField’ to the JSON of each one\nfor (const item of $input.all()) {\n item.json.myNewField = 1; // Augmenting the payload\n console.log("In code node: item: " + JSON.stringify(item.json, 2));\n}\n\nconsole.log("$input.params: " + JSON.stringify($input.params, 2));\n\nreturn $input.all();”
},
“type”: “n8n-nodes-base.code”,
“typeVersion”: 2,
“position”: [
1040,
280
],
“id”: “4d6493ea-4bd9-438a-b6aa-55e62b754984”,
“name”: “Code”
}
],
“connections”: {},
“pinData”: {},
“meta”: {
“instanceId”: “f10109f7dc4079894d1c86f62c9389babdb908f9640695d286f88e9e8c90153a”
}
}

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.80.5
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • **Running n8n via: local install **
  • **Operating system: Windows 11 **

The working directory where n8n runs is /home/node. Have you tried placing your file in that directory? The relative imports should resolve relative to that folder.

I’m on Windows 11. Where is /home/node?

Trying logging out process.cwd() to find that.

Sorry, to clarify, when you run console.log(process.cwd()), you’d find the working directory where your Node.js code is running (this would be logged in browser console). So once you place your files in that folder, I believe you should be able to import them relatively.

/home/node is the working directory in Docker on Windows.

I have already tried that and it does not work.
It says process is undefined. It appears the sandbox is hiding things from global where process would be defined by default on a Node.js program.

I tried all the following without success:
// None of these are defined - so can’t find current dir
console.log("dirname from process: "+process.cwd());
console.log("filename: "+__filename);
console.log("dirname: "+__dirname);

That would happen when this is not correctly set:

When that variable is correctly set and Node.js built-ins are correctly available, process is automatically available. Have you tried setting the environment variables in Windows settings?

Open Start and search for Environment Variables, you’d find a screen where you can add it:

Yes I had already verified that the n8n process had these defined correctly. I use process explorer and here is the output:

There has to be something more fundamental that I’m missing. After all one would think this is a pretty common thing to do in a Code Node.And unfortunately the doc is pretty scant on this topic.

The variable does not have N8N_ prefix, it should be:

NODE_FUNCTION_ALLOW_BUILTIN=*

As for the directory, on Windows, I believe it would be the directory you’d get when you try to visit %USERPROFILE% in File Explorer.

Ah good catch.
I tried with the updated variables and I have it working when I place the module under node_modules. At least, I have a path forward with one solution. Thanks.

However, I still cannot get require working from a relative path.
That is using const myFct1 = require(‘./fcts/myFct’);

So, I used an execute command node to show what’s the current directory and it does show what I would expect: the dir from which n8n is started.

Question: does n8n use a custom require loader? Perhaps, that loader works differently than regular node.js require?

I managed to get it working as follows:

const { cwd } = require('node:process')
const { join } = require('node:path')

const { a } = require(join(cwd(), 'my-lib', 'a.js'))
console.log(a(1, 2))

My file (a.js) is in /home/node/my-lib and the code is:

function a(b, c) {
  return b + c
}

module.exports = {
  a
}

I correctly got 3 in console.

1 Like

thanks. That works.

So I experimented a bit more and it appears that to use require for a lib that is not under node_modules, we cannot use relative paths.

Essentially this works: const myLib = “/dev4/n8n/fcts/myFct”;
But this DOES NOT: const myLib = “./fcts/myFct”; // does not works
even though the current dir is /dev4/n8n

I’ll create a separate forum post to ask development if this indeed intended as it is quite opposite from usual JS conventions.

Thanks again.

1 Like

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