1

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) => {
        //log.error(err);
    });
}

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

4
  • What's this my-utils? Commented Sep 14, 2016 at 9:51
  • @NitzanTomer It's the name of my NPM module Commented Sep 14, 2016 at 9:52
  • Seems like node can't find your module. Look at the compiled output, does it make sense? Can you include that in your question? Commented Sep 14, 2016 at 10:15
  • 1
    @NitzanTomer the answer was to remove the "default" keyword. See the accepted solution. Commented Sep 14, 2016 at 11:00

1 Answer 1

12

Try changing

export default function sendEmail

to just

export function sendEmail
Sign up to request clarification or add additional context in comments.

3 Comments

That was precisely what I did. Removed all default declarations, even before seeing your answer and everything stated to work as expected. Tks
Voted down! What about calling the util method? Why did not to add its usage?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.