DEV Community

Shmagi
Shmagi

Posted on

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:

Hello, I am New here, And also in development, I just made my first Website, using the MERN stack, Now want to deploy it on the digital ocean, the frontend part is working ok, my Nginx configuration is ok, but I have a problem with backend, my server.js file, is not working, it is causing this error ' Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: '

I am using the import and export ES module, and also have "type": "module" in my package.json,

The app is working ok on a local server,

Can anyone help? :))

here is my server.js file

========================

import path from 'path'
import express from 'express'
import dotenv from 'dotenv'
import colors from 'colors'
import { notFound, errorHandler } from './middleware/errorMiddlleware.js'
import connectDB from './config/db.js'
import eventRouter from './routes/eventsRoute.js'
import blogRouter from './routes/blogRoute.js'
import userRouter from './routes/userRoutes.js'
import uploadRouter from './routes/uploadRoutes.js'

dotenv.config()
connectDB()
const app = express()
app.use(express.json())

app.use('/api/events', eventRouter)
app.use('/api/blogs', blogRouter)
app.use('/api/users', userRouter)
app.use('/api/uploads', uploadRouter)

const dirname = path.resolve()
app.use('/uploads', express.static(path.join(
dirname, '/uploads')))

if(process.env.NODE_ENV === 'production'){

app.use(express.static(path.join(__dirname, '/frontend/build')))

app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'))
})
} else {
app.get('/', (req, res) => {
res.send('App Is Running...')
})
}

app.use(notFound)
app.use(errorHandler)

const PORT = process.env.PORT || 5000

app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});

app.listen(5000, () => {
console.log(Server Is Running In ${process.env.NODE_ENV} Mode On Port ${PORT}.yellow.bold)
})

==========================

Top comments (1)

Collapse
 
seanolad profile image
Sean

I'm gonna give some advice, dev.to is mainly for sharing ideas and posts. Not for helping solve your problems, so for fast and efficient help I suggest going on stack overflow to see if someone has had the same problem as you. If not be the first to ask for help there and I guarantee you'll get an answer in 30 minutes. 😎 Hope you get a solution.