Can I require jsx files in node?

Viewed 5326

I have a script that does some analysis on my source files and a part of that analysis is to require the file. Some of the files are in JSX format however and node does not understand this by default.

Is it possible to make it so that a file that looks like this:

function MyModule () {
  return <div>hello</div>
}

module.exports = MyModule

is possible to require through require('./my-module')?

1 Answers

Use JSX as a template engine in Node

NPM Package : https://www.npmjs.com/package/jsx-node

To be able to simply require .jsx files, you need to tell Node what to do with them. Running the following code makes you able to require('./SomeFile.jsx'):

require('jsx-node').install({
  replace: {
    preact: 'jsx-node',
  }
});

Warning:

This module is still in a very early phase. Any production use should be approached with caution.

For more Detail visit Link.

Related