javascriptnode.jssendinblue

Error importing SendInBlue in my NodeJS app?


I am refactoring my Node app to use ES6 import modules instead of requiring in files.

I am unable to figure out how to make this work with my e-mail package SendInBlue because I get the error.

TypeError: Cannot read properties of undefined (reading 'ApiClient')

Before I was using "import { SibApiV3Sdk } from "sib-api-v3-sdk" but I got the error:

SyntaxError: Named export 'SibApiV3Sdk' not found. The requested module 'sib-api-v3-sdk' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'sib-api-v3-sdk';
const { SibApiV3Sdk } = pkg;

^ So this is why I am using the current import code below, which is not working either?

How can I get this to work?

CODE

import pkg from "sib-api-v3-sdk"
const { SibApiV3Sdk } = pkg"
const defaultClient = SibApiV3Sdk.ApiClient.instance
const apiKey = defaultClient.authentications["api-key"]
apiKey.apiKey = process.env.SEND_IN_BLUE_API_KEY
import secretCodeHtml from "../templates/secretCodeEmail.js"

const sendSecretCodeEmail = (text) => {
  var apiInstance = new SibApiV3Sdk.TransactionalEmailsApi()
  var sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail()
  sendSmtpEmail = {
    sender: { email: senderEmail },
    to: [
      {
        email: recipientEmail,
        name: recipientEmail,
      },
    ],
    subject: emailSubject,
    htmlContent: text,
  }
  apiInstance.sendTransacEmail(sendSmtpEmail)
}

export default sendSecretCodeEmail

ERROR

file:///Users/app/git/app-node-api/src/emails/create/sendSecretCodeEmail.js:4
const defaultClient = SibApiV3Sdk.ApiClient.instance
                                  ^
TypeError: Cannot read properties of undefined (reading 'ApiClient')
    at file:///Users/app/git/app-node-api/src/emails/create/sendSecretCodeEmail.js:4:35
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)


Solution

  • in this case use an alias on the import like this:

    import * as sib from "sib-api-v3-sdk";