简体   繁体   English

从Node JS中的另一个模块导入函数

[英]Importing function from another module in node js

Importing certain function from some folder which has needed module with this function doesn't work. 无法从需要此模块的模块的文件夹中导入某些功能。

I'm using nodemailer for sending emails. 我正在使用nodemailer发送电子邮件。 I have 3 different folders with modules. 我有3个带有模块的文件夹。 The problem is in importing (require) email sending function to current module from another one. 问题在于从另一模块导入(要求)电子邮件发送功能到当前模块。 It becomes undefined and error is myFunc is not a function . 它变得undefined ,错误是myFunc is not a function I'm doing pretty simple things like requiring function from folder with index.js which includes needed function. 我正在做一些非常简单的事情,例如从index.js文件夹中要求功能,其中包括所需的功能。 But it becomes undefined when I try to use it. 但是当我尝试使用它时,它变得不确定。

services/mailTransport.js 服务/ mailTransport.js

const nodemailer = require('nodemailer');

const mailTransporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
     secure: false, 
    auth: {
      user: 'test@test.com',
      pass: 'myPassword'
    }
});

module.exports = mailTransporter;

services/index.js 服务/ index.js

const mailTransporter = require("./mailTransporter");
module.exports = { mailTransporter }

utils/mailTemplate.js utils的/ mailTemplate.js

const { mailTransporter } = require("../services");

const sendEmail = function (obj, msg) {
return new Promise( (res, rej) => {
    let mailOptions = {
        from: 'test@test.com',
        to: `${obj.to}`,
        subject: `${obj.subject}`,
        text: "plain text",
        html: "<b>" + msg + "</b>"
    };
    mailTransporter.sendMail(mailOptions, (error, info) => {
        mailTransporter.close();
        if (error) {
            rej(error);
        }
    console.log('Message sent: %s', info.messageId);
    res(info.messageId);
    });
})
}

module.exports = { sendEmail };

And finally I want to use it here in projects/emails.js 最后我想在project / emails.js中使用它

const { sendEmail } = require("../utils/mailTemplate");
const { vendorNotificationMessage } = require("../utils/emailMessages");

async function notifyVendors(steps) {
try {
    for(let step of steps) {
        if(step.vendor) {
            const message = vendorNotificationMessage(step);
            step.to = step.vendor.email;
            step.subject = "Step cancelling notification!";
            await sendEmail(step, message);
        }
    }
} catch(err) {
    console.log(err);
    console.log("Error in notifyVendors");
}
}

module.exports = { notifyVendors };

I expect that email will be sent using that sendEmail function. 我希望使用该sendEmail函数发送电子邮件。 But it stops with the error TypeError: sendEmail is not a function . 但是它以错误TypeError: sendEmail is not a function停止TypeError: sendEmail is not a function

The correct syntax for exporting something from a module is 从模块导出内容的正确语法是

exports.globalObjectName = localObjectName

So in your first file the export statement should look like this 因此,在第一个文件中,export语句应如下所示

exports.mailTransporter = mailTransporter

I do not think that you need {} when you use module.exports and require. 我认为在使用module.exports并要求时不需要{}。 Try module.exports = sendEmail ; 试试module.exports = sendEmail ; and const sendEmail = require("../utils/mailTemplate"); const sendEmail = require("../utils/mailTemplate");

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

相关问题 如何使用express,ejs将function从a.js模块导入node js中的another.js模块 - how to import function from a .js module to another .js module in node js, using express, ejs 在node.js中,如何从app.js中的另一个模块中的模块访问函数? - In node.js how do I access a function from app.js that is in a module that is in another module? node.js从另一个模块所需的模块中获取stub异步函数 - node.js proxyquire stub asynchronous function from module required by another module 节点js从客户端和另一个节点模块调用相同的延迟函数 - node js calling same deferred function from client and another node module 即使没有导入,导入模块 node.js 函数也会覆盖另一个函数 - importing modules node.js function overrides another function even without importing 我不能使用从 node.js 中的另一个模块导出的 function - I cannot use an exported function from another module in node.js 在 node.js 中导入模块失败 - Failed importing a module in node.js 导入作为 Node 模块安装的 JS 库 - Importing a JS library installed as a Node module 如何将Node.js中的此函数导出到另一个模块 - How to export this function in Node.js into another module 从模块内调用Node.js模块函数 - Calling a Node.js module function from within the module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM