babel-plugin-transform-es2015-block-scoped-functions

raw JSON →
6.22.0 verified Sat Apr 25 auth: no javascript deprecated

Babel plugin (v6.22.0) that transforms function declarations inside blocks to be block-scoped, ensuring compatibility with ES2015 semantics in older environments. This plugin is part of Babel's deprecated 'es2015' preset and is no longer maintained independently. It converts block-level function declarations to variable assignments with the same name, preventing hoisting issues. For modern Babel usage (v7+), use @babel/plugin-transform-block-scoped-functions instead.

error Error: Cannot find module 'babel-plugin-transform-es2015-block-scoped-functions'
cause Package not installed or using wrong Babel version.
fix
npm install babel-plugin-transform-es2015-block-scoped-functions --save-dev (for Babel 6) or use @babel/plugin-transform-block-scoped-functions (for Babel 7+)
error Plugin/Preset files are not allowed to export objects, only functions
cause Using the plugin with Babel v7 which expects @babel/plugin prefix.
fix
Switch to @babel/plugin-transform-block-scoped-functions.
error Error: Invalid plugin "transform-es2015-block-scoped-functions" specified in
cause Typo or wrong configuration format.
fix
Ensure plugin name is correct and in quotes: "transform-es2015-block-scoped-functions"
deprecated This package is deprecated as of Babel v7. Use @babel/plugin-transform-block-scoped-functions instead.
fix Replace with @babel/plugin-transform-block-scoped-functions in Babel v7 projects.
breaking Plugin renaming: babel-plugin-transform-es2015-block-scoped-functions has been renamed to @babel/plugin-transform-block-scoped-functions in Babel v7.
fix Use @babel/plugin-transform-block-scoped-functions or include in @babel/preset-env (which automatically includes it when needed).
gotcha Does not work with Babel v7 or later; only compatible with babel-core v6.
fix Upgrade to @babel/core v7 and use @babel/plugin-transform-block-scoped-functions.
gotcha Plugin only applies to function declarations at block level; does not affect arrow functions or function expressions.
fix No fix needed; understand the scope of the plugin.
npm install babel-plugin-transform-es2015-block-scoped-functions
yarn add babel-plugin-transform-es2015-block-scoped-functions
pnpm add babel-plugin-transform-es2015-block-scoped-functions

Shows how to configure and use the plugin to block-scope function declarations within blocks.

// .babelrc
{
  "plugins": ["transform-es2015-block-scoped-functions"]
}

// Input
function f() { return 1; }
{
  function f() { return 2; }
}
f(); // now returns 1 instead of 2 in older engines

// Output (transformed)
function f() { return 1; }
{
  var _f = function f() { return 2; };
}
f(); // returns 1

// Via Node API
var babel = require('babel-core');
var result = babel.transform('{ function foo() {} }', {
  plugins: ['transform-es2015-block-scoped-functions']
});
console.log(result.code);