Having trouble with importing SendInBlue into my NodeJS application?

Currently in the process of updating my Node app to utilize ES6 import modules over requiring files.

Encountering difficulties while trying to integrate this change with SendInBlue for email functionality, resulting in the following error message:

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

Priorly, using "import { SibApiV3Sdk } from "sib-api-v3-sdk" which led to 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;

^ Hence, the current import code that I'm now implementing is also encountering issues?

Seeking guidance on resolving this matter.

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)

Answer №1

If you encounter a similar situation, consider using an alias when importing as shown below:

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

Answer №2

Try using the following code snippet:

const package =  require('sib-api-v3-sdk');

In case you encounter any type challenges, simply create a new file in the root directory of your project called index.d.ts. Then, insert the following line:

declare module 'sib-api-v3-sdk'

Save the file and proceed with your project.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to check if specific HTML tags or text are present in the response of my Express app?

Currently, I have an Express Node server set up and I am using Mocha and Supertest to conduct route testing. One of the requirements I have is to verify the presence of specific text in the response from one of my Express routes. Here's a sample snip ...

The occurrence of unexpected errors persists when using npx create-express-api

Whenever I attempt to utilize create-express-api in CMD, I keep encountering errors for unknown reasons. When initiated through git CMD: Cloning into 'server'... events.js:292 throw er; // Unhandled 'error' event ^ Error: ...

Animated smooth updates in d3 line graphs are the key to creating dynamic and

I'm attempting to modify an example of Animated Line Graphs from: http://bl.ocks.org/benjchristensen/1148374 <div id="graph1" class="aGraph" style="width:600px; height:60px;"></div> <script> function draw(id, width, height, upd ...

Guide on building a REST API with Node.js Express.js and OracleDB to perform database updates

Looking for assistance with creating an API in Node.js to fetch and update data on the frontend. I am able to retrieve data using the provided code, but facing challenges updating it. React.js is being used for the frontend. var express = require("expr ...

The Apache Cordova application developed in Visual Studio 2015 is experiencing technical difficulties

When attempting to run my first Apache Cordova app in Visual Studio 2015 on Windows 8, I encountered an error. Despite trying some solutions, such as cleaning the Cordova cache in the options menu, the error persisted. Below is the error message that needs ...

Instructions on dynamically positioning a collection of div elements at the center of a webpage container

I am looking to create a set of divs that are centered on the page and adjust in size according to the length of the username. .Container1 { display: table; width: 100%; padding:0; margin:0; -webkit-box-sizing: border-box; -moz-box-sizing: bor ...

Change the background color according to the user's input text

I want to create a text box where users can input color keywords like blue, lime, or black. When they click submit, I want the background color of the page to change accordingly. This is what I have so far: <label for="color">Color: </label> ...

AngularJS Banner: Displaying Current Calendar Week and Increasing by 10 Days

I'm brand new to Angular and currently encountering some issues. Here's what I'm trying to create: I need to display the current Date: yyyy-MM-ss (Functional) I want to show the current Calendar Week: yyyy-Www (Not Working) When a butto ...

jQuery not being applied to dynamically added dropdown element

I am currently utilizing bootstrap and jquery within my react project. I have a button that, when clicked, should transform into a dropdown field. The dropdown functions properly when placed statically, but the functionality is lost once it is dynamically ...

Looking for a solution to correct an Enoent error that occurred while trying to install Zurb Foundation for Apps via the command line?

After installing Node.js and updating my $PATH environment, I encountered persistent errors while attempting to install Zurb Foundation for Apps. If anyone has any suggestions on how to resolve this issue, I would greatly appreciate it. Thank you! The com ...

Refreshing the Mocha Server

Recently, I encountered a situation where I needed to automate some cleanup tasks in my Express server using Mocha tests. In my server.js file, I included the following code snippet to manage the cleanup operations when the server shuts down gracefully (s ...

Filtering Quantity Based on Specific Attribute in ReactJs

I want to implement a quantity filter that can be based on color, size, or both. For instance, when I select the red color, it should display the total quantity of red items available. However, if I choose both the color red and size small, it should show ...

Showcasing a graphical representation with the help of Highcharts

I am currently exploring JavaScript and trying to familiarize myself with interactive charts using the HighCharts library. Unfortunately, I have been facing some challenges in getting the graph to print correctly. Despite attempting various examples, none ...

Unable to modify variable values in AngularJS

I'm currently utilizing AngularJS along with the "Angular Material" implementation (found here: https://material.angularjs.org/latest/#/) One of the components I'm using is the SideNav component: https://material.angularjs.org/latest/#/demo/mate ...

Incorporating a React element into a JavaScript object's property: A comprehensive guide

Below is a React Element named Info that has been attached to a Javascript object named myObj: let Info = ( <Info type="green" /> ); let myObj = { ReactComp: Info }; Now, the goal is to render the Info component using the above myObj objec ...

Ways to detect and respond to events in this particular scenario

I'm creating necessary components dynamically based on the provided CSS. This process involves iterating through a response array and generating HTML elements accordingly. for (var i = 0; i < responseinner.length; i++) { for (var k = 0; k < ...

How come it's not possible to modify the text of this button right when the function kicks off?

When I click a button, it triggers a JavaScript function. The first line of code within the function uses jQuery to change the HTML of the button. However, the button's text does not update in the browser until after the entire function has completed, ...

What is the best way to keep an image centered in the nav-bar as it decreases in size?

As I work on building a website using a template (https://github.com/learning-zone/web-templates/tree/master/victory-school-free-html5-bootstrap-template), I encountered an issue with the navigation bar. The original design appears like this: Before shrin ...

Transform a dynamic web page into a static one using Next.js

Utilizing Next.js and React.js, I create dynamic web pages. Specifically, I generate user-related web pages that need to be updated when there are changes in the user data. How can I generate static HTML pages from this updated data within Next.js? Are the ...

Utilize Proxy API for routing before directing to Angular application with Nginx

If you were to visit the following URL: https://example.com/AbCDeFghijK ...and waited a few milliseconds while AbCDeFghijK is verified by an API, you would then be redirected to: https://example.com/?ref=SomeString How can this be achieved if you are u ...