'Why I am getting this error: declares '<MODULE>' locally, but it is not exported
I have a typescript file which exports a function to send emails using aws ses
//ses.tsx
let sendEmail = (args: sendmailParamsType) => {
let params = {
//here I get the params from args, then I send the email
};
return AWS_SES.sendEmail(params).promise();
};
module.exports = {
sendEmail
};
then somewhere else I have a next.js API endpoint page importing the module like this
//index.tsx
import {sendEmail} from "../../../lib/ses";
the path is correct but the editor (phpstorm) highlight that line above in red and the error message is
declares 'sendEmail' locally, but it is not exported.
The path is correct and as you can see the module is exported. When I execute the page I don't get any error but the email is not sent.
Also, I use the same module from another endpoint and it works. Any suggestion to solve or debug this?
Solution 1:[1]
You're mixing up ES with CommonJS.
module.exports is used with require() //CommonJS Syntax
export is used with import //ES Syntax.
you can either export all the objects at once
Oor you can simple export the object directly.
Mail.tsx:
export const sendEmail = () => {}
main.tsx
import {sendEmail} from "./Mail";
Also I recommend to use const instead of let for functions.
export default ...
also works, but may be used one time per file and can be imported with
import Mail from "./Mail.tsx";
Mail.sendEmail(...);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ven |