LoginSignup
12
10

More than 5 years have passed since last update.

[babel-cli] babelのコマンドライン実行( es6 to es5)

Posted at

npmのバージョンは下記の通り

$ npm -v
2.10.1

babel 6.X.Xから、インストールパッケージは babel ではなく babel-cliになったとのこと

インストール

# 作業ディレクトリで初期化
$npm init

# babel-cliのインストール
$npm install --save-dev babel-cli

# このままでは変換できないので、
# es5への変換に必要なプラグインが一通りはいった
# プリセットをインストール
# https://babeljs.io/docs/plugins/preset-es2015/
$npm install --save-dev babel-preset-es2015

このあと.babelrcを作成して、
es2015プリセットを使用すると教えてあげる

.babelrc

{
    "presets": ["es2015"]
}

テスト用es6でかかれたファイルを用意

test.js
const obj = (() => {
  return {
    method() {
      alert('Hello Babel!');
    }
  };
})();

変換

$ ./node_modules/.bin/babel test.js
'use strict';

var obj = function () {
  return {
    method: function method() {
      alert('Hello Babel!');
    }
  };
}();

無事変換されました

12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10