sendgrid-nodejs icon indicating copy to clipboard operation
sendgrid-nodejs copied to clipboard

Axios is not a function. (In 'axios(data)', 'axios' is an instance of Module) - when trying to send mail

Open dansmog opened this issue 10 months ago • 2 comments

Image

I've been getting this error when i try to send emails.

So i did a set up on digital ocean droplet, installed nginx and every other thing i need.

API works fine, but when it gets to sending emails it fails

This is my logic for sending email

`import 'dotenv/config'; import { sendGridConfig } from '../config/config'; import sgMail from '@sendgrid/mail';

sgMail.setApiKey(sendGridConfig.apiKey);

export const sendMail = async ( to: string, name: string, data: any, templateId: string ) => { try { await sgMail.send({ to, from: sendGridConfig.sender, templateId, dynamic_template_data: data, }); console.log("Email Sent Successfully"); } catch (err) { console.error("SendGrid Error:", err.response?.body || err.message); throw new Error("Email Failed to Send"); } };`

Sendrid mail version => 7.7.0 downgraded from 8.8.0 both version fails

Nodejs version => v20.16.0 Nginx version => nginx/1.26.0 (Ubuntu) Yarn version => 1.22.22

dansmog avatar Jan 24 '25 07:01 dansmog

Im having the same issue

milbin avatar Feb 14 '25 01:02 milbin

The issue for me was because the library uses CommonJS format to import axios when my project was using ES modules.

Changing this line in the code fixed it for me: https://github.com/k----n/sendgrid-nodejs/commit/395f861d313ee0cb08ae3a62b9b28164bc24d328#diff-e5eefeb8ae9201959d1fdd93f319727cf0b472f694b8af15d931f99fd004b8aeL2

I isolated the changes to a branch and installed it with npm and everything works now: npm i git+https://github.com/k----n/sendgrid-nodejs.git#client

k----n avatar Apr 02 '25 09:04 k----n