简体   繁体   English

为什么我收到此错误:声明&#39;<MODULE> &#39; 在本地,但不会导出

[英]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我有一个打字稿文件,它导出一个使用 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然后在其他地方我有一个 next.js API 端点页面,像这样导入模块

//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路径是正确的,但编辑器(phpstorm)以红色突出显示上面的那一行,错误消息是

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?有什么建议可以解决或调试这个问题吗?

You're mixing up ES with CommonJS.您将 ES 与 CommonJS 混为一谈。

module.exports is used with require() //CommonJS Syntax module.exports 与 require() 一起使用 //CommonJS 语法

export is used with import //ES Syntax. export 与 import //ES 语法一起使用。

you can either export all the objects at once您可以一次导出所有对象

Oor you can simple export the object directly.或者您可以直接简单地导出对象。

Mail.tsx:邮件.tsx:

export const sendEmail = () => {}

main.tsx主要的.tsx

import {sendEmail} from "./Mail";

Also I recommend to use const instead of let for functions.我还建议使用 const 而不是 let 函数。

export default ...

also works, but may be used one time per file and can be imported with也可以,但每个文件可以使用一次,并且可以使用

import Mail from "./Mail.tsx";
Mail.sendEmail(...);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在此React / Typescript应用程序中收到no exported member错误? - Why am I getting the no exported member error in this React/Typescript app? TSLint 抛出 'error TS2459: Module '"@azure/core-tracing"' 在本地声明 'Span',但未导出。' 和其他错误 - TSLint is throwing 'error TS2459: Module '"@azure/core-tracing"' declares 'Span' locally, but it is not exported.' and other errors 为什么收到错误“未捕获的SecurityError:无法在&#39;HTMLCanvasElement&#39;上执行&#39;toDataURL&#39;:错误的画布可能无法导出。 ” - Why am I getting the error “Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported. ” 为什么我会收到“找不到模块 &#39;jsonLogic&#39;”错误? - Why am I getting a "Cannot find module 'jsonLogic'" error? 为什么我收到 Webpack 'module not found' 错误? - Why am I getting Webpack 'module not found' error? 当所有文件都在本地位于同一目录中时,为什么会出现同源策略错误? - Why am I getting a Same-origin policy error while all files are locally in the same directory? 为什么会出现此错误? - Why am I getting this error? 为什么在使用 cloudinary 模块时出现“语法错误:无法在模块外使用导入语句”错误? - Why am I getting the 'SyntaxError: Cannot use import statement outside a module' error while using the cloudinary module? 我得到了,TypeError: Node Function not defined 但需要和模块导出? 我该怎么办? - I am getting, TypeError: Node Function not defined but required and module exported? What should I do? 我收到此错误“没有模块:ngResource” - I am getting this error “No module: ngResource”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM