How to Enable ES6 Features in Node.js with Babel: A Practical Guide
This article explains how Node.js supports various ES6 features, categorizes them into shipping, staged, and in‑progress groups, and shows how to enable full ES6 syntax in Node.js projects using Babel plugins, configuration files, and build scripts.
Because Babel, WebPack and React are popular, developers like Xiao Ming have started using many ES2015 (ES6) features such as import, export, class, arrow functions, let and const, but Node.js still rejects import statements, leading to inconsistent code styles in the same repository.
Node.js Support for ES6
Node.js’s V8 engine classifies ES6 features into three levels: shipping (enabled by default), staged (requires the --harmony flag), and in‑progress (still experimental).
Shipping (enabled by default)
Block scoping
let (strict mode only)
const
function‑in‑blocks (strict mode only)
Classes (strict mode only)
Collections, Map, WeakMap, Set, WeakSet
Typed arrays
Generators
Binary and Octal literals
Object literal extensions (shorthand properties and methods)
Promises
New String methods
Symbols
Template strings
Arrow Functions
new.target
Object.assign
Spread operator
Staged (requires --harmony)
Symbol.toStringTag
Array.prototype.includes (polyfill available in 5.x)
Rest Parameters (transform‑es2015‑parameters can transpile, 5.x)
In‑progress (experimental)
--harmony_modules (enable "harmony modules")
--harmony_array_includes (enable "harmony Array.prototype.includes")
--harmony_regexps (enable "harmony regular expression extensions")
--harmony_proxies (enable "harmony proxies")
--harmony_sloppy (enable "harmony features in sloppy mode")
--harmony_unicode_regexps (enable "harmony unicode regexps")
--harmony_reflect (enable "harmony Reflect API")
--harmony_destructuring (enable "harmony destructuring")
--harmony_sharedarraybuffer (enable "harmony sharedarraybuffer")
--harmony_atomics (enable "harmony atomics")
--harmony_new_target (enable "harmony new.target")Enabling ES6 in Node.js with Babel
Babel can transpile ES6 syntax that the current engine does not support. The typical workflow is to select a set of Babel plugins that cover the needed features.
Recommended plugins
transform‑strict‑mode (adds "use strict" automatically)
transform‑es2015‑modules‑commonjs (converts ES6 modules to CommonJS)
transform‑es2015‑spread (adds spread operator support)
transform‑es2015‑destructuring (adds destructuring support)
transform‑es2015‑parameters (adds default parameters, parameter destructuring, etc.)
Conversion example: import
import Mod from './mod';
new Mod();transforms to
'use strict';
var _mod = require('./mod');
var _mod2 = _interopRequireDefault(_mod);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
new _mod2.default();Conversion example: export
export default class Mod { }transforms to
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
class Mod { }
exports.default = Mod;Installation
# Install core and CLI
$ npm install --save-dev babel-core babel-cli
# Install all plugins
$ npm install --save-dev babel-plugin-transform-strict-mode babel-plugin-transform-es2015-modules-commonjs babel-plugin-transform-es2015-spread babel-plugin-transform-es2015-destructuring babel-plugin-transform-es2015-parameters.babelrc configuration
{
"plugins": [
"transform-strict-mode",
"transform-es2015-modules-commonjs",
"transform-es2015-spread",
"transform-es2015-destructuring",
"transform-es2015-parameters"
]
}Project structure and build commands
Place ES6 source files in a src directory and compile them to lib: babel src --out-dir lib During development you can run the code directly with:
babel-node src/index.jspackage.json scripts
{
"name": "my-awesome-es6-package",
"version": "1.0.0",
"description": "use es6 in node",
"main": "lib/index.js",
"scripts": {
"start": "babel-node src/index.js",
"build": "babel src --out-dir lib"
},
"devDependencies": {
"babel-cli": "~6.3.17",
"babel-core": "~6.3.26",
"babel-plugin-syntax-object-rest-spread": "^6.3.13",
"babel-plugin-transform-es2015-modules-commonjs": "^6.3.16",
"babel-plugin-transform-es2015-spread": "^6.3.14",
"babel-plugin-transform-object-rest-spread": "^6.3.13",
"babel-polyfill": "^6.3.14"
}
}Remember to point the main field to the compiled file for end‑users, and avoid using babel-node in production.
Further reading
Node.js ES6 support documentation
Babel plugin list
ES6 compatibility tables
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Node Underground
No language is immortal—Node.js isn’t either—but thoughtful reflection is priceless. This underground community for Node.js enthusiasts was started by Taobao’s Front‑End Team (FED) to share our original insights and viewpoints from working with Node.js. Follow us. BTW, we’re hiring.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
