Solution for @babel/preset-react is installed but still doesn’t parse JSX
is Given Below:
I am writing an express server and all I want is for one of the endpoints to render a React component on the server-side and return the result. At the moment, I’m getting stuck with this error:
res.send(renderToString(<h1>hello world</h1>))
^
SyntaxError: Unexpected token '<'
I’m trying to run the script with babel-node and based my approach on the strategies outlined in this thread from a few years ago. I installed @babel/preset-react
but to no avail.
.babelrc
{
"presets": ["@babel/preset-env", ["@babel/preset-react", {
"development": true
}]
],
"plugins": ["@loadable/babel-plugin"]
}
Start script
"scripts": {
"start": "nodemon --exec babel-node src/server.js"
}
Endpoint
app.get('/dev/edit', (req, res) => {
const { dehydrated } = req.body
console.log('Received edit request.')
// top of file: import { renderToString } from 'react-dom/server.js'
res.send(renderToString(<h1>hello world</h1>))
})
Any help to would be very much appreciated!