Zed_Blade Zed_Blade - 5 months ago 83
Node.js Question

TypeScript export/import function

I have a module I wrote (which is installed via npm from a private repo) that the following structure:

email.ts, utils.ts, index.ts, (some other files)

On my email.ts I have the following function:

export default function sendEmail(toEmail: string, subject: string, content: string): any {

let helper = SendGrid.mail;

let from_email: any = new helper.Email(process.env.FROM_EMAIL);
let to_email: any = new helper.Email(toEmail);
let helperContent: any = new helper.Content("text/plain", content);
let mail: any = new helper.Mail(from_email, subject, to_email, helperContent)

var request = SendGrid.emptyRequest({
method: "POST",
path: "/v3/mail/send",
body: mail.toJSON()

//This performs the request with a promise
SendGrid.API(request).then((response: any) => {
//Deal with output as needed
console.log("email was sent!!");
}).catch((err: any) => {

Then, on my index.ts I have the following declarations:

export * from "./email";
export * from "./errors";
export * from "./logger";
export * from "./objectUtils";
export * from "./queryFilter";
export * from "./templateEngine";
export * from "./utils";

On the application where I import this module I import this as

import * as Utils from "my-utils";

Finally, whenever I want to use the sendEmail function, I call it by using this statement:

Utils.sendEmail(email, subject, content);

However this always throws an error stating that
"Cannot read property 'sendEmail' of undefined"

Why is this happening? Shouldn't I be able to use this type of declarations when exporting this way?
What is the solution for this?

Best Regards


Try changing

export default function sendEmail

to just

export function sendEmail