TypeScript ESLint Issue - Const required instead of import?

my code works perfectly fine for an openaiClient.ts in an api folder in a pages folder in a src folder of a repo.

when i go to yarn build, i get a warning that its unsafe to call the api via “require”.

if i change it to a standard import, then it crashes due to TypeError (expression not constructable).

const { OpenAIApi } = import(“openai”); //works great
import openai from ‘openai’; // can’t run

it would be a huge leap forward if I could deploy this api app, it has a ton of benefit to developers haha

am i missing something simple, or overlooking an obvious thing?

thanks!

image

By chance, have you tried importing the OpenAIApi class like this:

import { OpenAIApi } from 'openai';

If that still shows compilation errors, could you share your tsconfig.json file?

absolutely! thank you for helping me.

see below

{
  "compilerOptions": {
    "target": "es2017",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "checkJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "noUncheckedIndexedAccess": true,
    "baseUrl": ".",
    "paths": {
      "~/*": ["./src/*"]
    }
  },
  "include": [
    ".eslintrc.cjs",
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "**/*.cjs",
    "**/*.mjs"
  ],
  "exclude": ["node_modules"]
}