libby
libby

Reputation: 634

How to "require" a JS class that contains JSX?

I'm trying to call functions of my JS class which extends react. I have one file RunTest.js which looks like this:

let Builder = require('./Builder');
let b = Builder();
b.say("Hello World!");

My Builder.js in the same directory looks something like this:

class Builder extends React.Component{
    ...
}

module.exports = { Builder }

And when I run with node RunTest.js I receive the following error:

bash-3.2$ node RunTest.js
/Users/jonahlibrach/Documents/project/prototypes/bron/v1/java/prototype/
server/static/protocol/Builder.js:322
                        <div class="navbar">
                        ^

SyntaxError: Unexpected token <
    at Module._compile (internal/modules/cjs/loader.js:743:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:810
:10)
    at Module.load (internal/modules/cjs/loader.js:666:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:606:12)
    at Function.Module._load (internal/modules/cjs/loader.js:598:3)
    at Module.require (internal/modules/cjs/loader.js:705:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.<anonymous> (/Users/jonahlibrach/Documents/project/prototy
pes/bron/v1/java/prototype/server/static/protocol/RunTest.js:1:15)
    at Module._compile (internal/modules/cjs/loader.js:799:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:810
:10)

How do I use this class in node?

Upvotes: 0

Views: 33

Answers (1)

Quentin
Quentin

Reputation: 944054

You cannot use modules that contain JSX in Node.js without first transpiling them to JavaScript (e.g. with Babel).

Upvotes: 3

Related Questions