Node.js JSX Require Hook
node-jsx is a module designed to enable transparent `require()` of JSX files directly within Node.js environments. Published over a decade ago, its primary function was to transpile JSX syntax on-the-fly using Facebook's jstransform (an early JSX processing tool) so that Node.js could execute `.jsx` files without a prior build step. The package, currently at version 0.13.3, has been officially superseded by modern build pipelines involving tools like Babel, Webpack, Vite, or TypeScript's built-in JSX transformation capabilities. Its last known update was in April 2015, indicating it is no longer maintained. Modern JavaScript and React development workflows universally rely on dedicated transpilers and bundlers for JSX processing, making `node-jsx` an obsolete solution incompatible with contemporary ecosystems.
Common errors
-
Error: Cannot find module 'react/lib/ReactContext'
cause Attempting to use `node-jsx` with modern React versions (>=0.14) which changed internal module structures significantly.fixUpgrade your JSX transpilation pipeline entirely. `node-jsx` is incompatible with modern React. Use Babel or TypeScript instead. -
SyntaxError: Unexpected token '<'
cause Node.js attempting to parse a `.jsx` file directly without the `node-jsx` hook being correctly installed or because the file type isn't registered for transpilation.fixEnsure `require('node-jsx').install()` is executed *before* any `.jsx` files are `require()`d. However, this only applies to the severely outdated setup. For modern projects, this error means your build/transpilation step is misconfigured. -
TypeError: Cannot read properties of undefined (reading 'install')
cause The `require('node-jsx')` call did not return an object with an `install` method. This could be due to a faulty installation or an attempt to use `import` with a CommonJS-only package.fixVerify that `node-jsx` is correctly installed (`npm install node-jsx`). Ensure you are using `require('node-jsx').install()` and not `import` syntax.
Warnings
- breaking This package is officially deprecated and completely unmaintained since 2015. It is incompatible with modern React versions (>=0.14) and contemporary JavaScript/JSX syntax.
- breaking The `harmony: true` option mentioned in its README for ES6 transforms is severely outdated and will not correctly process modern ES6+ or JSX features (e.g., Fragments, Hooks, spread attributes).
- gotcha This package exclusively supports CommonJS (`require()`) and cannot be used with ES Modules (`import`). Node.js projects using ES Modules will not be able to utilize this hook.
- gotcha Using `node-jsx` for on-the-fly transpilation in production Node.js applications will incur significant performance overhead, as every `require()` of a JSX file involves a parsing and transformation step.
Install
-
npm install node-jsx -
yarn add node-jsx -
pnpm add node-jsx
Imports
- install
import { install } from 'node-jsx';require('node-jsx').install(); - installWithOptions
const jsx = require('node-jsx'); jsx.install({ ... });require('node-jsx').install({ extension: '.jsx', harmony: true });
Quickstart
const path = require('path');
const fs = require('fs');
// Install the node-jsx require hook
require('node-jsx').install({ harmony: true });
// Imagine a simple JSX component file at src/MyComponent.jsx
// const React = require('react');
// module.exports = function MyComponent({ name }) { return <div>Hello, {name}!</div>; };
// This package is for JSX transpilation only, it does not include React itself.
// Trying to require modern React or a JSX file directly will likely fail
// due to incompatibilities and missing React runtime. This example demonstrates
// the setup, but requires a legacy React setup to actually run JSX.
// For demonstration, let's pretend a legacy React is available.
// In a real modern scenario, this setup is completely obsolete.
console.log('node-jsx require hook installed. This setup is highly deprecated.');
console.log('Modern projects use Babel/TypeScript for JSX transpilation.');