Registry / web-framework / babel-plugin-react-native-web

babel-plugin-react-native-web

JSON →
library0.21.2jsnpmunverified

The `babel-plugin-react-native-web` package is a Babel plugin designed to optimize applications using `react-native-web`. It works by aliasing `react-native` imports to `react-native-web` and performing tree-shaking to exclude unused modules, thereby reducing bundle size. The current stable version is 0.21.2. This plugin is part of the `react-native-web` ecosystem, which generally sees frequent updates, often tied to new React and React Native versions, indicating an active development and release cadence. Its primary differentiator is enabling existing React Native codebases to run efficiently on the web without significant code changes, by providing a targeted build-time optimization layer. It's crucial for projects aiming for a single codebase across native and web platforms.

npm install babel-plugin-react-native-web
INSTALL
IMPORT
SIG · BABEL-PLUGIN-REACT
B
babel-plugin-react-native-web
web-frameworkjavascriptv0.21.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Babel Plugin Configuration
plugins: [ ["react-native-web", { commonjs: false }] ]
import plugin from 'babel-plugin-react-native-web'; // Incorrect usage pattern plugins: [plugin]
Babel plugins are configured in `.babelrc` or `babel.config.js` via an array, not imported like standard modules. The `commonjs` option should match your bundler's module resolution.
React Native Imports (Pre-transform)
import { StyleSheet, View } from 'react-native';
import StyleSheet from 'react-native-web/dist/exports/StyleSheet'; // Don't manually import internal paths import View from 'react-native-web/dist/exports/View';
The plugin's purpose is to rewrite `react-native` imports to optimized `react-native-web` paths. Developers should continue to import from `react-native` in their source code.

This quickstart demonstrates configuring the `babel-plugin-react-native-web` plugin in a Babel configuration file to enable aliasing and tree-shaking for React Native for Web projects.

// .babelrc or babel.config.js module.exports = { presets: ['module:metro-react-native-babel-preset'], // Or other React/TypeScript presets plugins: [ // ... other plugins ["react-native-web", { // Set to true if your bundler (e.g., Webpack 4 without module: false) // resolves CommonJS modules by default. Most modern bundlers use ES modules. commonjs: false }] ] };
Debug
Known issues
breakingReact 19 support was introduced in v0.20.0, which also removed `findNodeHandle` support on web. Projects updating to v0.20.0 or higher must ensure compatibility with React 19 and deprecation of `findNodeHandle`.
fix
Upgrade React to v19 or compatible version. Refactor code that relies on `findNodeHandle` for web, as it's no longer supported.
affects: >=0.20.0
breakingBrowser support was reduced in v0.18.0, requiring Safari 10.1+, Edge (Chromium), and dropping support for IE and legacy Android browsers. Additionally, styles are now inserted on module eval, impacting server-side rendering.
fix
Ensure target browser matrix aligns with the new requirements. Review server-side rendering setup for style injection changes.
affects: >=0.18.0
breakingReact 18 `createRoot` support and related changes (e.g., `Animated`, `ScrollView` as Class component) were introduced in v0.19.0. Projects using React 18 must ensure their components and usages are compatible.
fix
Upgrade React to v18 or a compatible version. Verify `Animated` and `ScrollView` implementations for React 18 compatibility.
affects: >=0.19.0
breakingUnstable APIs had breaking changes in v0.17.0, specifically regarding `accessibilityRole='menuitem'` and `accessibilityRole='link'` behavior, and `unstable_createElement` inference. These changes affect accessibility and custom element creation.
fix
Review and update usage of `accessibilityRole` props and `unstable_createElement` to align with the new behaviors, particularly for accessibility features.
affects: >=0.17.0
breaking`Dimensions`, `Animated`, `VirtualizedList`, and `NativeEventEmitter` had breaking changes in v0.16.0 due to updates from React Native. `NativeEventEmitter` no longer inherits `EventEmitter`.
fix
Update implementations relying on `Dimensions`, `Animated`, `VirtualizedList`. For `NativeEventEmitter`, replace `removeSubscription` with the subscription object's `remove()` method.
affects: >=0.16.0
gotchaThe `commonjs` option in the plugin configuration (e.g., `['react-native-web', { commonjs: true }]`) must match your bundler's module resolution strategy. Misconfiguration can lead to incorrect module resolution or larger bundles.
fix
Set `commonjs: true` only if your bundler is configured to use CommonJS modules by default for `react-native-web` (e.g., older Webpack configs or specific module resolutions). Most modern bundlers support ES modules, so `commonjs: false` (or omitting the option) is often correct.
affects: All versions
breakingReact 17 became a peer dependency in v0.15.0. Also, `I18nManager` API changed, requiring `getConstants()` to access `isRTL` and `doLeftAndRightSwapInRTL`.
fix
Ensure `react` and `react-dom` are at least version 17. Update `I18nManager` access patterns: `I18nManager.getConstants().isRTL`.
affects: >=0.15.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'commonjs') when configuring babel-plugin-react-native-web
The plugin configuration is malformed, or the options object is missing or not a plain object.
fix
Ensure the plugin configuration is an array with the plugin name as the first element and an options object as the second element, e.g., `['react-native-web', { commonjs: false }]`.
Error: `react-native-web` internal paths are not stable and you must not rely on them.
Directly importing from `react-native-web/dist/exports/...` instead of `react-native` in your source code.
fix
Always import components and APIs from `react-native` (e.g., `import { View, Text } from 'react-native';`). The Babel plugin will handle the internal path rewriting automatically and reliably.
Upgrade
Version history
0.21.2latest on npm
Audit
Dependencies
react-native-webrequiredThis plugin aliases imports to `react-native-web`, which must be installed as a peer/runtime dependency of the project.
reactrequiredReact is a peer dependency of `react-native-web` (v0.15+ requires React 17+, v0.19+ supports React 18, v0.20+ supports React 19).
@babel/corerequiredAs a Babel plugin, Babel itself is required for its operation.
Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
babel-plugin-react-native-web — npm install babel-plugin-react-native-web · libregistry