hi Guys.
I have used both nodejs 16 & 18. whenever I try to deploy new version, I get error in my console "Uncaught SyntaxError: unexpected Token '<' " and it shows the page as blank.
If I erase all history and cache OR hard refresh (CTRL + F5) ( 1 time process) after that it works fine as a normal website no need to do anything.
How I Deploy MERN App
Nodejs main file (index.js) codes as below
ErrorLog and folder tree pics uploaded.
Hey, I am facing the same error. Have you resolved it?
Not yet
This following will work if you setup your express js as module type.
import express from 'express'
import path from 'node:path'
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const app = express()
app.disable('x-powered-by')
app.use(cors())
app.use(express.json())
app.use(bodyParser.json())
app.use(cookieParser())
const __dirname = dirname(fileURLToPath(import.meta.url));
const root = path.join(__dirname, 'build')
app.use(express.static(root))
app.get("*",(req, res) => {
res.sendFile(path.resolve(root, 'index.html'));
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}...`);
});
You can change other settings that will suit you.