Installing module nodemailer

Describe the problem/error/question

I am trying to use nodemailer for emails as I need to use proxies to connect to smtp.
include it in a node code as follows:
const nodemailer = require(‘nodemailer’);

The error I am getting when executing this node:
Cannot find module ‘nodemailer’ [line 1]

What is the error message (if any)?

Cannot find module ‘nodemailer’ [line 1]

Additional information

I have tried a million things by now. I have created a Dockerfile and package.json on github.

Latest Dockerfile content:
FROM n8nio/n8n:latest

USER root

WORKDIR /home/node/.n8n/nodes

RUN npm install -g [email protected] https-proxy-agent --unsafe-perm

USER node

Package.json content:

{

“name”: “n8n-custom-nodes”,
“private”: true,
“dependencies”: {
“nodemailer”: “^6.10.01”,
“https-proxy-agent”: “^7.0.4”
}
}

in Coolify I have set two environment variables N8N_NODE_FUNCTION_ALLOW_EXTERNAL and NODE_FUNCTION_ALLOW_EXTERNAL that both have value nodemailer,https-proxy-agent

I have ran npm install nodemailer in /.n8n/nodes/
In the coolify ssh terminal I can see it’s nodemailer is installed in /.n8n/nodes/node_module.

I have also tried setting a node_path variable in the environment variable in the n8n resource.

Nothing worked, 6 hours wasted.

Information on your n8n setup

  • n8n version: 1.113.3 (communite edition)
  • Database (default: SQLite): PostGre
  • n8n EXECUTIONS_PROCESS setting (default: own, main): ?
  • Running n8n via (Docker, npm, n8n cloud, desktop app): coolify on vultr

@rickv are you sure the module is been installed in the correct place?

Both packages are installed with n8n already, is there a reason you want to install them again?

At this point, I am not sure if anything hah. What do you suggest:

A) how I can check which path n8n checks for modules

B) how I can check if the module is in that location (I assume go there with SSH and if there is a directory “nodemailer”, that should be installed?)

No reason at all, if installed even better. But that doesn’t make sense, because I got the error that nodemailer was not found when I add thia in a code node:

const nodemailer = require(‘nodemailer’);

If it was installed by default, I shouldn’t have received that error right?

not if your NODE_FUNCTION_ALLOW_EXTERNAL isn’t set or isn’t properly set.

Please add an Execute Command node and run the following command:

echo $NODE_FUNCTION_ALLOW_EXTERNAL

Then please tell us what the output is.

It gives me no output (I also tried console.log($NODE_FUNCTION_ALLOW_EXTERNAL)

When I hover over the variable it says:
Cannot find name ‘$NODE_FUNCTION_ALLOW_EXTERNAL’.

I do have this in the n8n build in coolify:

I did run this code:
// This code uses the standard Node.js process object to read
// the environment variable.
const allowedModules = process.env.NODE_FUNCTION_ALLOW_EXTERNAL;

// Check if the variable is defined
if (allowedModules) {
console.log(NODE_FUNCTION_ALLOW_EXTERNAL is set to: ${allowedModules});
} else {
console.log(‘NODE_FUNCTION_ALLOW_EXTERNAL is NOT set.’);
}

// Return the value as output
return [{ json: {
NODE_FUNCTION_ALLOW_EXTERNAL: allowedModules,
status: allowedModules ? ‘Set’ : ‘Not Set’
} }];

And the output is:
[Node: “Code in JavaScript”] NODE_FUNCTION_ALLOW_EXTERNAL is NOT set.

So apparently that’s the issue, which is weird as it’s specifically set as seen in the screenshot above.

it cannot give NO output at all, even if the variable is not set, there will still be output.

that wouldn’t work, only the following would and I think only if you explicitly allow it with another env var

console.log($env.NODE_FUNCTION_ALLOW_EXTERNAL)

Did you reboot after setting these?

Sorry you asked for the execute command node. I’ve done that now:

[

{

“exitCode”: 0,

“stderr”: “”,

“stdout”: “”

}

]

Yes I did reboot after those settings (a million times by now hah)

Yeah, this means that the variable is definitely not set.

Because I was so sure I set it, I googled differently and found this:

“for compose based projects/services you need to add the env to the compose file in the ui, this will create the desired entries on the env tab for you to fill in, it doesn’t work the otehr way around”

Which is the solution. It now works!

thank you for your help!

1 Like

Great insight

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