Babel 6 JSX Transform Plugin
This package, `babel-plugin-transform-react-jsx` version 6.24.1, is the legacy plugin for transforming JSX syntax into `React.createElement` calls (or custom pragma calls) within Babel 6 projects. It was the standard way to process JSX for JavaScript applications using Babel's previous major version. This plugin is no longer actively maintained and has been superseded by `@babel/plugin-transform-react-jsx` (and often integrated via `@babel/preset-react`) in Babel 7 and later. Projects currently on Babel 7 or 8 should use the scoped `@babel/plugin-transform-react-jsx` to ensure compatibility with modern React runtimes (classic or automatic) and the latest JavaScript features. This package is relevant only for maintaining older codebases that are specifically pinned to Babel 6.
Common errors
-
ReferenceError: React is not defined
cause JSX is being transformed to `React.createElement` but `React` is not imported or globally available in the scope where JSX is used.fixEnsure `import React from 'react';` is present at the top of any file containing JSX, or that `React` is otherwise accessible in the global scope. -
Error: Plugin 'transform-react-jsx' not found. It might be a typo or it was removed in the latest version.
cause Using `babel-plugin-transform-react-jsx` with a Babel 7+ installation, which expects `@babel/plugin-transform-react-jsx`.fixInstall `@babel/plugin-transform-react-jsx` (`npm install --save-dev @babel/plugin-transform-react-jsx`) and update your Babel configuration to use `'@babel/transform-react-jsx'` or `@babel/preset-react`. -
Invalid option 'useBuiltIns'. Did you mean 'useSpread'?
cause The `useBuiltIns` option for `babel-plugin-transform-react-jsx` (v6) is a boolean that controls whether to use `Object.assign` directly. The error might indicate a typo or misuse of options specific to other Babel versions or plugins.fixReview the documentation for `babel-plugin-transform-react-jsx` v6 and ensure `useBuiltIns` is a boolean. If you are targeting modern JavaScript environments where `Object.assign` is natively available, setting it to `true` is usually fine. If the error persists and you are *not* using Babel 6, check the options for `@babel/plugin-transform-react-jsx` which might differ.
Warnings
- breaking This `babel-plugin-transform-react-jsx` package is for Babel 6 and is considered abandoned. For Babel 7 and newer projects, you MUST use the `@babel/plugin-transform-react-jsx` package (or `@babel/preset-react`) for correct and up-to-date JSX transformation. Using this older package with Babel 7+ will lead to configuration errors or unexpected behavior.
- breaking Babel 7 introduced a new `automatic` runtime for JSX transformations, which does not require `React` to be in scope. This `babel-plugin-transform-react-jsx` (v6) only supports the `classic` runtime, which explicitly converts JSX to `React.createElement` calls, requiring `React` to be imported or globally available.
- deprecated The `@jsx React.DOM` pragma comment, previously used to specify the JSX factory, was deprecated as of React v0.12 and is not recommended. While Babel 6 might still process it, relying on it is outdated.
- gotcha Confusing `pragma` and `pragmaFrag` options. This plugin (v6) only supports the `pragma` option for element creation. The `pragmaFrag` option for fragments was introduced in Babel 7's `@babel/plugin-transform-react-jsx` to support `React.Fragment` syntax.
Install
-
npm install babel-plugin-transform-react-jsx -
yarn add babel-plugin-transform-react-jsx -
pnpm add babel-plugin-transform-react-jsx
Imports
- babel-plugin-transform-react-jsx
import transformReactJSX from 'babel-plugin-transform-react-jsx'; // Plugins are configured, not imported in source code
{ "plugins": ["transform-react-jsx"] } - transform
import { transform } from 'babel-core'; // Babel 6 primarily used CommonJS for programmatic APIconst babel = require('babel-core'); const code = 'const element = <div>Hello</div>;'; babel.transform(code, { plugins: ['transform-react-jsx'] }); - options.pragma
{ "plugins": [ ["transform-react-jsx", { "pragma": "h" }] ] }
Quickstart
{
"name": "my-babel-6-app",
"version": "1.0.0",
"devDependencies": {
"babel-cli": "^6.0.0",
"babel-core": "^6.0.0",
"babel-plugin-transform-react-jsx": "^6.0.0"
},
"scripts": {
"build": "babel src --out-dir lib"
}
}
// .babelrc
{
"plugins": [
["transform-react-jsx", {
"pragma": "React.createElement",
"useBuiltIns": true
}]
]
}
// src/app.js
import React from 'react';
function App() {
return (
<div className="container">
<h1>Hello, Babel 6 JSX!</h1>
<p>This is an example of JSX transformed by `babel-plugin-transform-react-jsx`.</p>
</div>
);
}
export default App;