简体   繁体   English

如何在Node.js中正确导出函数?

[英]How do I export a function correctly in nodejs?

When I try to invoke my serverless function I get Type Error login is not a function , I believe there is something I am not doing correctly when exporting my function, 当我尝试调用无服务器函数时,我得到Type Error login is not a function ,我认为在导出函数时我做错了一些事情,

I am trying to arrange my project so I can import all of the functions in the utils/ folder easily I have a directory structure like this: 我正在尝试安排我的项目,以便我可以轻松地将所有功能导入utils/文件夹中,我有这样的目录结构:

handler.js
utils/
  login.js
  index.js

handler.js file handler.js文件

"use strict";
const {login} = require("./utils");

const username = 'user';
const password = 'pass';

module.exports.scrape = (event, context, callback) => {
  login(username, password)
      .catch(error =>
      callback(new Error(`Error scraping ${event}: ${JSON.stringify(error)}`))
    );
};

index.js file index.js文件

module.exports = {
    login: require("./login"),
  };

login.js file login.js文件

async function login(username, password) {
    // my function does something
};

module.exports.login = login;

In handler.js file, get login object while requiring utils 在handler.js文件中,在需要utils的同时获取登录对象

const login = require("./utils").login; const login = require(“ ./ utils”)。login;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM