Express app cannot find module OpenAIProvider

Hi!
I’m trying to deploy a simple api with express.
I have my entrypoint on /api/index.ts

// /api/index.ts
import app from '../src/app';
export default app;

The app for my api is on /src/app.ts

// src/app.ts
import express from 'express';
import cors from 'cors';
import { OpenAIProvider } from './providers/OpenAIProvider';
import chatRouter from './routes/chat';

const app = express();

// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('public'));

// Crear instancia del provider
const openAIProvider = new OpenAIProvider(process.env.OPENAI_API_KEY || '');

// Usar el router directamente ya que tiene su propia instancia del provider
app.use('/api/chat', chatRouter);

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

export default app;

Also, I have a simple html file with some vanilla js to test the API on /public/index.html

After deploy, the index.html interface is accesible but when I try to use the api I have this error on log section.

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/src/providers/OpenAIProvider' imported from /var/task/src/app.js

My tsconfig.json

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ES2020",
    "moduleResolution": "node",
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true,
    "baseUrl": "."
  },
  "include": [
    "api/**/*",
    "src/**/*"
  ],
  "exclude": ["node_modules"]
}

My vercel.json

{
  "version": 2,
  "builds": [
    {
      "src": "src/app.ts",
      "use": "@vercel/node",
      "config": {
        "includeFiles": [
          "src/**/*",
          "tsconfig.json"
        ]
      }
    },
    {
      "src": "public/**",
      "use": "@vercel/static"
    }
  ],
  "routes": [
    {
      "src": "/api/(.*)",
      "dest": "src/app.ts"
    },
    {
      "src": "/(.*)",
      "dest": "public/$1"
    }
  ]
}

I will apreciate a lot any help from the comunity.
thanks in advance!

If you’re having trouble deploying an Express app, this guide can help.

You can also ask v0 for suggestions tailored to your own project setup.

Hey, @herokid! Did you manage to get a solution to this? Did you look at our express guide?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.