Issue Deploying Express Backend with OpenAI API Connection

Hi everyone! I am facing an issue while trying to deploy my Express backend, which includes a connection with the OpenAI API. The problem arises when I import the OpenAI package, leading to the following error:

internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module 'node:fs'

If anyone in the community has encountered a similar problem or has insights on how to resolve this issue, your assistance would be greatly appreciated.

Thank you!

Welcome to the forum.

Are all your libraries and dependencies up to date?

Code example you can show us?

Hello Paul!

I’ve updated all libraries and packages, but the problem remains. The code is straightforward, breaking at the start.

const PORT = 8000
const express = require('express')
const cors = require('cors')
const axios = require('axios')
require('dotenv').config()

const OpenAI = require('openai');


const app = express()

If I comment the import of the openai module, the code works fine

Thanks!

Hi everyone!

Quick update: I’ve successfully set up a separate backend project, and it’s up and running. However, I’ve noticed a quirk – the server file exclusively works with .mjs files, allowing the use of import statements.

It is a bad practice?

Thanks a bunch!

What hosting service are you using to deploy?