简体   繁体   中英

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,

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:

handler.js
utils/
  login.js
  index.js

handler.js file

"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

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

login.js file

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

module.exports.login = login;

In handler.js file, get login object while requiring utils

const login = require("./utils").login;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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