JavaScriptでのnpmモジュールのインポート

独自のモジュールだけでなく、npmを通じてインストールされたモジュールも接続できます。この際、 独自のモジュールの前には./を指定する必要がありますが、npmモジュールの場合は必要ありません。

例を見てみましょう。underscoreライブラリをインストールします:

terminalnpm install underscore

次に、これを接続します:

src/index.jsimport _ from 'underscore';

接続したライブラリのいくつかの機能を利用してみましょう:

src/index.jsimport _ from 'underscore';

let arr = [1, 2, 3, 4, 5]
let res = _.first(arr) + _.last(arr);
console.log(res);

すべての関数ではなく、必要なものだけをインポートすることもできます:

src/index.jsimport {first, last} from 'underscore';

let arr = [1, 2, 3, 4, 5]
let res = first(arr) + last(arr);
console.log(res);
Задание 1

lodashライブラリをインストールしてください。 それをプロジェクトに接続し、このライブラリからいくつかのメソッドを使用してください。