This article introduces the installation process of Babel6.x~First of all, you can use Babel to convert online https://babeljs.io/repl/
Then enter the topic: Install Babel (command line environment, for Babel6.x version)
1. First install babel-cli (used to use babel in terminal)
npm install -g babel-cli
2. Then install the babel-preset-es2015 plug-in
npm install --save babel-preset-es2015
Note: The Babel5 version contains various conversion plugins by default, but Babel6.x related conversion plugins need to be downloaded by yourself, such as transform-es2015-arrow-functions, transform-es2015-classes, etc., and ES2015 preset contains all plugins. If no plug-in is installed, then converting on the command line will have no effect!
Among them, the --save parameter automatically updates the package.json file and writes it into the dependencies
3. Enter on the command line:
babel es6.js --presets es2015
Output:
"use strict"; [1, 2, 3].map(function (x) { return x * x; });Note: The following parameter --presets es2015 means using this plugin for compilation. If you do not write an upconversion, it will have no effect.
4. Plug-in configuration
It is very troublesome to write this parameter every time. You can create a new configuration file.babelrc in the current directory.
However, in Windows system, it is not allowed to directly right-click to create files without file names. You can create them through the cmd command line: Open cmd in the current folder and type a command
type nul>.babelrc
You can create a file.babelrc in the current directory, and then write it to the file:
{ "presets": ['es2015'] }Then you can use babel es6.js directly on the command line to convert without adding parameters indicating the plugin used
In addition to creating .babelrc files, you can also configure them in package.json and add the following properties:
"babel": { "presets": ["es2015"]}Attached with Babel common commands:
1. Convert es6.js file and output it in the current named line program window
babel es6.js
2. Convert es6.js and output it to es5.js file (using -o or --out-file)
babel es6.js -o es5.js
babel es6.js --out-file es5.js
3. Real-time monitoring es6.js will be recompiled as soon as there is any change (using -w or --watch)
babel es6.js -w --out-file es5.js
babel es6.js --watch --out-file es5.js
4. Compile the entire src folder and output it to the lib folder (using -d or --out-dir)
babel src -d lib
babel src --out-dir lib
5. Compile the entire src folder and output it to a file
babel src --out-file es5.js
6. Directly enter the babel-node command, and you can run ES6 code directly from the command line
babel-node
The above is the correct installation process of ES6 Babel in JavaScript introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!