FBJS Utilities
FBJS (Facebook JavaScript) is a collection of utility libraries developed by Facebook primarily for internal consumption within their JavaScript projects, such as React and Relay. Currently at version 3.0.5, this package does not follow strict semantic versioning and APIs may change or disappear without public notice. It is explicitly not designed for broader public use, and external projects consuming it should anticipate instability and a lack of support. Its core purpose is to facilitate code sharing across Facebook's internal monorepo, leveraging a custom `@providesModule` system that is transformed into CommonJS `require('fbjs/lib/moduleName')` paths during a build step. There is no stated public release cadence, as its releases are tied directly to Facebook's internal development needs.
Common errors
-
Cannot find module 'emptyFunction'
cause Your build system is not configured to resolve `@providesModule` references from FBJS, or you are trying to use a non-existent module.fixEnsure your Babel configuration includes the FBJS `rewrite-modules` plugin, or explicitly `require('fbjs/lib/emptyFunction')`. However, external use is not recommended. -
TypeError: (0 , _warning.default) is not a function
cause Attempting to use a named export (like `warning`) as a default import in an ESM context, or incorrect interop with CommonJS modules.fixUse CommonJS `const warning = require('fbjs/lib/warning');` pattern, as FBJS primarily ships as CommonJS. Avoid ESM imports unless specifically compiled to work with FBJS. -
Invariant Violation: Value must not be null or undefined.
cause An `invariant` check failed, meaning a critical condition expected by the FBJS utility (or the calling code) was not met.fixReview the conditions leading to the invariant call and ensure all preconditions for the operation are satisfied before calling the FBJS utility.
Warnings
- breaking FBJS is not intended for public consumption and does not adhere to semantic versioning (semver). APIs may change, break, or be removed without notice, leading to frequent breaking changes in patch or minor versions.
- gotcha Official support and feature requests for FBJS are limited to internal Facebook projects. External projects will not receive support for issues or new features.
- gotcha FBJS heavily relies on a custom `@providesModule` system and Babel transforms (like `babel-preset/plugins/rewrite-modules.js`) to resolve module paths. Without this specific build setup, direct `require()` calls to relative paths like `emptyFunction` will fail.
Install
-
npm install fbjs -
yarn add fbjs -
pnpm add fbjs
Imports
- emptyFunction
import { emptyFunction } from 'fbjs/lib/emptyFunction';const emptyFunction = require('fbjs/lib/emptyFunction'); - warning
import warning from 'fbjs/lib/warning';
const warning = require('fbjs/lib/warning'); - invariant
import { invariant } from 'fbjs';const invariant = require('fbjs/lib/invariant');
Quickstart
// This example demonstrates direct CommonJS usage of FBJS utilities.
// Note: FBJS is primarily intended for internal Facebook projects and
// is not guaranteed to follow semver or provide stable public APIs.
const emptyFunction = require('fbjs/lib/emptyFunction');
const warning = require('fbjs/lib/warning');
const invariant = require('fbjs/lib/invariant');
const identity = require('fbjs/lib/identity');
console.log('Calling emptyFunction:', emptyFunction()); // Should print undefined
let shouldWarn = true;
warning(
!shouldWarn, // Condition for warning to trigger (true means no warning, false means warning)
'This is a development warning from FBJS. Condition was %s.', // Message
shouldWarn // Args for message formatting
);
// invariant example (will throw if condition is false)
try {
const value = null;
invariant(value != null, 'Value must not be null or undefined.');
console.log('Value is:', value);
} catch (e) {
console.error('Invariant failed:', e.message);
}
console.log('Identity of 123 is:', identity(123)); // Should print 123