Javascript:为什么要获取 aTypeError:xxxx 不是在编译时使用 exports/require 的函数

标签 javascript import export

我使用导出函数:

sendSmtpMessage.js

'use strict';
const nodemailer = require("nodemailer");
const { OAuth2Client } = require('google-auth-library');

exports.sendSmtpMessage = function(keys, mailOptions) {
  ...
  return true;
};

然后我在我的 index.js 文件中导入并使用它:

索引.js

const sendSmtpMessage = require("./sendSmtpMessage.js");
...
sendSmtpMessage(keys,mailOptions); // <=== error

编译出错

TypeError: sendSmtpMessage is not a function

最佳答案

您将其作为默认 导出导入 - 将其导入为命名导入(就像解构对象一样):

const { sendSmtpMessage } = require("./sendSmtpMessage.js");

或者,像以前一样导入它,但分配给 module.exports 而不是分配给 exports 的属性:

module.exports = function(keys, mailOptions) {
  ...
  return true;
};

关于Javascript:为什么要获取 aTypeError:xxxx 不是在编译时使用 exports/require 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53776560/

相关文章:

javascript - 如何在使用 create-react-app 创建的应用程序中使用具有访问 token 的 API

python - 如何从 python 中没有 __init__.py 的文件夹导入文件?

python - 我收到消息 : "Bad import syntax:" when I try to import a python function inside my gnuradio flowgraph

sql-server - SQL Server 2008中如何写入文本文件

postgresql - 使用 pg_restore 从较新版本的 PostgreSQL 恢复

database - 导出数据库 Oracle PL/SQL

javascript - 添加动态YouTube对象时,参数不起作用(JavaScript)

javascript - 你认为 iframe 的替代品是什么

javascript - 如何使用单击事件处理程序在点之间画线?

MySQL:导入时忽略错误?