Netlify Functions are not build correctly

Hello Everyone, hope you’re doing great :smiley:

Probably this is a noob error and really hope it is.
I’m trying to make a Netlify function, every function I wrote was working except for this one.
Is working on local but when trying to build or deploy it always through an error of non encountered modules.

When I built the Function myself I can see that the src directory are not including my modules. It’s just the Function Page:

var Mailing = require(’…/usecases/Mailing’);
var Invitation = require(’…/usecases/Invitations’);
var NetlifyIdentity = require(’…/Lib/Identity’);
var User = require(’…/usecases/Users’);
var Books = require(’…/usecases/Books’);

var templateBookPurchased = require(’…/mails/BookPurchased’);
var welcomeTemplate = require(’…/mails/NewAccount’);
var templatePackagePurchased = require(’…/mails/PackagePurchased’);

exports.handler = async (event, context) => {
console.log(Mailing);
let headers = {
‘Access-Control-Allow-Origin’: ‘’,
‘Access-Control-Allow-Headers’: '
’,
‘Access-Control-Allow-Methods’: ‘*’,
};

if (event.httpMethod === ‘OPTIONS’) {
return { statusCode: 200, headers, body: ‘Method Allowed’ };
}

if (event.httpMethod === ‘GET’) {
return { statusCode: 200, headers, body: ‘Method Allowed’ };
}

let eventBody = await JSON.parse(event.body);

let {
data: { object: paymentObject },
type,
} = eventBody;

if (
type !== ‘checkout.session.completed’ &&
paymentObject.status !== ‘paid’
) {
return {
statusCode: 500,
headers,
body: ‘Go away buddy!’,
};
}

try {
console.log(Conecting database);
await dbConnect();
console.log(Database connected);
} catch (error) {
return { statusCode: 500, headers, body: JSON.stringify(error) };
}

try {
const invitation = await Invitation.getByMail(user.email);

const appUsers = await NetlifyIdentity.getAllUsers({ context });

let user = appUsers.find(
  (user) => user.email === paymentObject.customer_details.email
);

if (paymentObject.metadata.package) {
  let packages = Books.getAllPackages();

  let package = packages.find(
    (package) => package.title === paymentObject.metadata.title
  );

  let books = package.books;

  if (!invitation.id && !user.id) {
    await Invitation.create({
      mail: paymentObject.customer_details.email,
      userType: 'free',
      books: books.map(({ contentfulId, includeAudioBook }) => ({
        contentfulId,
        includeAudioBook,
      })),
    });
  } else if (user.id) {
    for (let book of books) {
      await User.pushBook(user.id, {
        contentfulId: book.contentfulId,
        includeAudioBook: book.includeAudioBook,
      });
    }
  }

  var sendinBlueResponse = await Mailing.sendTransactionalMail({
    to: [{ email: paymentObject.customer_details.email }],
    htmlContent: templatePackagePurchased({
      package,
      image: package.mainImage.fields.file.url,
      price: packge.pricing,
      mail: paymentObject.customer_details.email,
    }),
    subject: `Thanks for your purchase`,
    tags: ['recipt'],
  });
} else {
  let books = await Books.getAll();

  let purchasedBook = books.find(
    (book) => book.title === paymentObject.metadata.title
  );

  if (!invitation.id) {
    var newInvitation = await Invitation.create({
      mail: paymentObject.customer_details.email,
      userType: 'free',
      books: [
        {
          ...purchasedBook,
          includeAudioBook: paymentObject.metadata.includeAudioBook,
        },
      ],
    });
  } else {
    await User.pushBook(user.id, {
      contentfulId: purchasedBook.contentfulId,
      includeAudioBook: purchasedBook.includeAudioBook,
    });
  }

  var sendinBlueResponse = await Mailing.sendTransactionalMail({
    to: [
      {
        email: paymentObject.customer_details.email,
      },
    ],
    htmlContent: templateBookPurchased({
      book: purchasedBook,
      image: purchasedBook.cover.fields.file.url,
      price: purchasedBook.pricing,
      mail: paymentObject.customer_details.email,
    }),
    subject: `Thanks for your purchase`,
    tags: ['recipt'],
  });
}

if (!invitation.id && user.id) {
  var newSendinBlueResponse = Mailing.sendTransactionalMail({
    to: [
      {
        email: paymentObject.customer_details.email,
      },
    ],
    htmlContent: welcomeTemplate({
      mail: paymentObject.customer_details.email,

      welcomeMessage: `
      Welcome to your new dashboard, here you can access your purchases inside Reader Zone.
      All your purchases are automatically added to your account
    `,
    }),
    subject: `Instructions to access your new digital products from 21stCenturySuperhuman`,
    tags: ['instructions'],
  });
}

return {
  statusCode: 200,
  headers,
  body: JSON.stringify({
    status: sendinBlueResponse,
    welcome: newSendinBlueResponse || 'No email to send',
  }),
};

} catch (error) {
console.log(’{ ERRORRRRRRRRRR => }:’, JSON.stringify(error));
console.log(error);

return {
  statusCode: 200,
  headers,
  error: JSON.stringify(error),
};

}
};

Hey there, @IrfDev :wave:

Welcome to the Netlify Forums! Thanks for sharing your question with us.
Did you have any luck making progress on this over the weekend?

If not, can you please share your Netlify site as well as any additional debugging steps you have taken between your first post and now? Thank you :slight_smile: