question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[SyntaxError] "Unexpected token <" [render(<App/>)]

See original GitHub issue

I’ve got this error when I launch my node server :

    content: render(<App/>)
                    ^
SyntaxError: Unexpected token <
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:425:7)
    at startup (bootstrap_node.js:146:9)

Here’s a sample of my server.js file (didn’t show all the requires for instance).
I don’t know what’s wrong. You can see I use preact-render-to-string and pass this rendering to my handlebar template.

The same technique is used there : https://github.com/jakearchibald/big-web-quiz/blob/master/server/views.js

const handlebars = require('handlebars');
const render = require('preact-render-to-string');
const index = require('../front/index');
/** @jsx h */

app.get('/', (req, res) => {
  res.send(index({
    css: ['/static/style.css'],
    lazyCss: ['/static/style.css'],
    scripts: ['/static/build.js'],
    content: render(<App/>)
  }));
});

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
developitcommented, Mar 20, 2017

Hi there - you need to transpile this with Babel, since you’re using the JSX Syntax. If you already have Babel set up, it’s likely you’re just missing the babel-plugin-transform-react-jsx plugin. In your .babelrc:

{
  "plugins": [
    ["transform-react-jsx", { "pragma": "h" }]
  ]
}

In the repo you linked, you can see where they run Babel with that plugin here: https://github.com/jakearchibald/big-web-quiz/blob/master/gulpfile.js#L108-L118

3reactions
developitcommented, Mar 20, 2017

There is: you can use the hyperscript function (h()) directly instead of JSX, or something like HyperX or t7, which use template strings instead of JSX. If this is the only file you’re not transpiling though, it’s super easy to change <App /> out for h(App):

const handlebars = require('handlebars');
const render = require('preact-render-to-string');
const index = require('../front/index');

app.get('/', (req, res) => {
  res.send(index({
    css: ['/static/style.css'],
    lazyCss: ['/static/style.css'],
    scripts: ['/static/build.js'],
    content: render( h(App) )   // h() usage
  }));
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactJS Module build failed: SyntaxError: Unexpected token
ReactDOM.render(<App />, container); is not valid JS.. · 1. @xDreamCoding You can use it in a JS file as long as the JSX...
Read more >
Uncaught SyntaxError: Unexpected token '<' (at app.js:12:12)
Hi devlopers i'am trying to setup inertia.js in laravel-react-vite app but the app showing blank page after deployment and the console...
Read more >
Unexpected token '<' with reactDom.render() : r/reactjs - Reddit
A community for learning and developing web applications using React ... Uncaught SyntaxError: Unexpected token '<' with reactDom.render().
Read more >
Uncaught SyntaxError: Unexpected token in JSX
I am using Parcel to build my React app. It's throwing a syntax error: unexpected token. HTML: <!DOCTYPE html> <html lang="en"> <head> <meta ......
Read more >
Unexpected token < - React app - Web - Zoom Developer Forum
Which version? “@zoomus/websdk”: “^1.7.10”,. To Reproduce(If applicable) Steps to reproduce the behavior: just after component render.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found