Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Amadeus is not a constructor #4

Open
SKTimTam opened this issue Dec 15, 2024 · 7 comments
Open

TypeError: Amadeus is not a constructor #4

SKTimTam opened this issue Dec 15, 2024 · 7 comments

Comments

@SKTimTam
Copy link

Hi hope you are doing well.

Getting this error when trying to use the the Amadeus constructor. when i run node --import=tsx server.ts

const amadeus = new Amadeus({
                ^
TypeError: Amadeus is not a constructor

below is my code

"use server";

import Amadeus, {
  ResponseError,
  FlightOffersSearchGetParams,
} from "amadeus-ts";

const amadeus = new Amadeus({
  clientId: process.env.API_KEY,
  clientSecret: process.env.API_SECURITY,
});
export async function getFlightOffers(value: FlightOffersSearchGetParams) {
  try {
    const response = await amadeus.shopping.flightOffersSearch.get(value);
    return response.result;
  } catch (error: unknown) {
    if (error instanceof ResponseError) {
      console.log(error);
    }
  }
}

Do you have suggestions on what could be wrong?

@darseen
Copy link
Owner

darseen commented Dec 15, 2024

Hi @SKTimTam, thanks for reaching out regarding this error!

It seems that this issue is not directly related to the library, my guess is because you are using the command node --import=tsx server.ts which could result in an unexpected behaviour maybe because you are using a server action in Next.js which would rely on you starting the Next.js project and loading env variables.
But still I do not know what the exact cause of this problem is as it's not related to the amadeus-ts library.

I can redirect you to a reference project I made to demonstrate how to use this library with Next.js 15. Let me know if you keep having errors when you start your project.

@darseen darseen closed this as not planned Won't fix, can't repro, duplicate, stale Jan 2, 2025
@observableobject
Copy link

I'm having this same problem! It's obvious that you did something incorrect in your library, because the simple solution in your readme doesn't work for express and node applications. I'm however, struggling to figure out why.

@observableobject
Copy link

This is incredibly sad, I'm going to have to go to sleep without solving the issue with the broken library :-(

@darseen
Copy link
Owner

darseen commented Jan 12, 2025

Can you provide me with the code that is causing the error? Because I just tested with a simple express app and everything seems to be working fine.

You can test this code on your machine and check if it causes the same problem, so I can know if it's an issue related to node.js version.

import express, { Request, Response } from "express";
import Amadeus, { FlightOffersSearchGetParams } from "amadeus-ts";
import dotenv from "dotenv";

// App setup
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
dotenv.config();

const amadeus = new Amadeus({
  clientId: process.env.AMADEUS_CLIENT_ID,
  clientSecret: process.env.AMADEUS_CLIENT_SECRET,
});

// Routes
app.post("/flights", async (req: Request, res: Response) => {
  const {
    adults,
    originLocationCode,
    destinationLocationCode,
    departureDate,
  }: FlightOffersSearchGetParams = req.body;

  const response = await amadeus.shopping.flightOffersSearch.get({
    originLocationCode,
    destinationLocationCode,
    departureDate,
    adults,
  });

  return res.status(200).json({ data: response.data });
});

// Connection setup
const PORT = process.env.PORT || 3001;

app.listen(PORT, () => {
  console.log(`Listening on port ${PORT}`);
});

@darseen darseen reopened this Jan 12, 2025
@observableobject
Copy link

Thanks for the quick reply! Unfortunately, I reset all my changes and went back to the JS npm package.

I have a feeling it could be related to our tsconfig?

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "outDir": "./build"
  },
  "include": ["src"],
  "exclude": ["node_modules"]
}

I did spend a lot of time trying to switch between require and import syntax, and applying "allowSyntheticDefaultImports": true to this.

We are on node 20.15 and

    "ts-node": "^10.9.2",
    "tsx": "^4.19.2",
    "typescript": "^5.7.2"

The error would happen not during compile time but runtime, as soon as the

const amadeus = new Amadeus({
  clientId: process.env.AMADEUS_CLIENT_ID,
  clientSecret: process.env.AMADEUS_CLIENT_SECRET,
});

is executed. I have placed it as a global property (error on npm start) and then moved into into the function call (error when function is called).

I even copied index.d.ts and index.js into our project and removed the package and still had the issue.

I imagine this is something quite simple, but I am a mobile developer so I'm not seeing it. Hopefully a config issue on our end but seems like a common configuration since I'm not the only one.

If I have some time later I'll try to give a minimum reproducible example if the config above doesn't provide any leads.

@darseen
Copy link
Owner

darseen commented Jan 13, 2025

Thank you for the details you provided. But unfortunately I haven't been able to reproduce the error you encountered even when using your tsconfig and nodejs 20.15.0. Still everything worked as expected!

It would be really helpful if you could provide a repo that has this issue so I can inspect it and hopefully manage to find the cause of this error.

@tresorama
Copy link

tresorama commented Jan 26, 2025

I'm affected also, in a express project.

A workaround is using this

import * as A from 'amadeus-ts';
import { AMADEUS_CLIENT_ID, AMADEUS_CLIENT_SECRET } from '@/constants';

const Amadeus = (A.default as any).default as typeof A.default;

export const amadeusClient = new Amadeus({
  clientId: AMADEUS_CLIENT_ID,
  clientSecret: AMADEUS_CLIENT_SECRET,
});

instead of

import Amadeus from "amadeus-ts";
import { AMADEUS_CLIENT_ID, AMADEUS_CLIENT_SECRET } from '@/constants';

export const amadeus = new Amadeus({
  clientId: AMADEUS_CLIENT_ID,
  clientSecret: AMADEUS_CLIENT_SECRET,
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants