babel-helper-modules
raw JSON → 6.0.0 verified Sat Apr 25 auth: no javascript
Provides Babel's internal helper functions (like _classCallCheck, _extends, etc.) as individual ES5 modules that can be imported directly. Version 6.0.0 is the latest stable release. Unlike babel-runtime which bundles core-js, this package avoids core-js dependencies, giving users freedom to choose their own polyfill (e.g., @babel/polyfill). Designed to be used with babel-plugin-transform-helper-modules to replace helper calls with direct imports, reducing bundle size and avoiding duplicate helper code. Supports only CommonJS require syntax and is intended for Node/Babel build pipelines.
Common errors
error SyntaxError: Cannot use import statement outside a module ↓
cause Trying to use ES import syntax with a CommonJS-only package.
fix
Change to
var helper = require('babel-helper-modules').helperName; error TypeError: _classCallCheck is not a function ↓
cause Incorrectly accessing the helper (e.g., importing default instead of named).
fix
Ensure you use
require('babel-helper-modules').classCallCheck and not require('babel-helper-modules') alone. error Uncaught SyntaxError: Unexpected token 'extends' ↓
cause Variable name 'extends' is reserved; using it as a variable name without renaming.
fix
Rename the imported helper:
var _extends = require('babel-helper-modules').extends; error Module not found: Error: Can't resolve 'babel-helper-modules' ↓
cause Package not installed or missing from node_modules.
fix
Run
npm install babel-helper-modules --save or yarn add babel-helper-modules. Warnings
gotcha Package is CommonJS-only; ESM import patterns (import/export) will fail. Only use require(). ↓
fix Use `var helper = require('babel-helper-modules').helperName;` instead of `import`.
gotcha The helper name 'extends' must be aliased when assigning because 'extends' is a reserved word in JavaScript. ↓
fix Assign to a different variable name: `var _extends = require('babel-helper-modules').extends;`
deprecated This package is a legacy artifact from Babel 6 era. Babel 7 uses @babel/helpers and @babel/runtime. New projects should prefer @babel/runtime with appropriate transform plugin. ↓
fix Migrate to @babel/runtime and @babel/plugin-transform-runtime for Babel 7+.
gotcha No TypeScript type definitions are provided. Using this package in a TypeScript project will require manual type declarations. ↓
fix Create a custom .d.ts file or use `require` with `@ts-ignore`.
gotcha Package does not include core-js polyfills; you must provide your own polyfill (e.g., @babel/polyfill) for instance methods like Array.prototype.find. ↓
fix Add @babel/polyfill or core-js to your project and import it early.
Install
npm install babel-helper-modules yarn add babel-helper-modules pnpm add babel-helper-modules Imports
- classCallCheck wrong
import { classCallCheck } from 'babel-helper-modules';correctvar classCallCheck = require('babel-helper-modules').classCallCheck; - extends wrong
import extends from 'babel-helper-modules';correctvar _extends = require('babel-helper-modules').extends; - objectWithoutProperties wrong
import { objectWithoutProperties } from 'babel-helper-modules';correctvar objectWithoutProperties = require('babel-helper-modules').objectWithoutProperties;
Quickstart
var classCallCheck = require('babel-helper-modules').classCallCheck;
var inherits = require('babel-helper-modules').inherits;
var extendsHelper = require('babel-helper-modules').extends;
var objectWithoutProperties = require('babel-helper-modules').objectWithoutProperties;
var possibleConstructorReturn = require('babel-helper-modules').possibleConstructorReturn;
// Example usage
function Child() {
classCallCheck(this, Child);
var self = possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this));
return self;
}
inherits(Child, Parent);
var _ref = someObject;
var a = _ref.a;
var rest = objectWithoutProperties(_ref, ['a']);
var newObj = extendsHelper({}, rest, { b: 2 });