Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

MERN STACK deploy using app engine. Uncaught Syntax Error. Uncaught token '<'

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

  1. There are 2 folders (Frontend: Client, Backend: server)
  2. Run "NPM run build" Script for get production files
  3. Copy "build" folder from client to "Server" page (folder tree pics uploaded)

Nodejs main file (index.js) codes as below

const express = require('express');
const path = require('path');
const app = express();

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

app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
})"Screenshot 2024-01-03 100918.pngScreenshot 2024-01-03 103741.png

ErrorLog and folder tree pics uploaded.

3 REPLIES 3

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. 

Top Labels in this Space
Top Solution Authors