Smile Engineering Blog

ジェイエスピーからTipsや技術特集、プロジェクト物語を発信します

Node.js のインストール

はじめに

node のバージョン管理に n を使っています。今までは n を npm で導入していて、そのためには先ず node/npm をシステムにインストールする必要があったのですが、これ無しに、直接 n をインストール方法を知ったので備忘のために記します。

直接インストールには n-install というサードパーティを使用します。

n-install

インストールはいたって簡単、以下を叩くだけです。

curl -L https://git.io/n-install | bash

ただし、事前に以下がセットアップされている必要があります(開発機であればまず入っているはず)。

$ curl -L https://git.io/n-install | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 39444  100 39444    0     0  34844      0  0:00:01  0:00:01 --:--:-- 4806k
===
You are ABOUT TO INSTALL n, the Node.js VERSION MANAGER, in:

  /home/jsp/n

Afterward, THE FOLLOWING Node.js VERSION(S) WILL BE INSTALLED,
and the first one listed will be made active; 
  'lts' refers to the LTS (long-term support) version, 
  'latest' to the latest available version.
  '-' means that no versions will be installed:

  lts
 
If your shell is Bash, Ksh, or Zsh, the relevant initialization file will be
modified in order to:
 - export environment variable $N_PREFIX.
 - ensure that $N_PREFIX/bin is in the $PATH
For any other shell you'll have to make these modifications yourself (details
to follow).

For more information, see https://git.io/n-install-repo
===
CONTINUE (y/N)? y
-- Cloning https://github.com/tj/n to '/home/jsp/n/n/.repo'...
-- Running local n installation to '/home/jsp/n/bin'...
-- Shell initialization file '/home/jsp/.bashrc' updated.
-- Installing helper scripts in '/home/jsp/n/bin'...
-- Installing the requested Node.js version(s)...
   1 of 1: lts...

  installing : node-v14.15.0
       mkdir : /home/jsp/n/n/versions/node/14.15.0
       fetch : https://nodejs.org/dist/v14.15.0/node-v14.15.0-linux-x64.tar.xz
   installed : v14.15.0 (with npm 6.14.8)

=== n successfully installed.
  The active Node.js version is: v14.15.0

  Run `n -h` for help.
  To update n later, run `n-update`.
  To uninstall, run `n-uninstall`.

  IMPORTANT: OPEN A NEW TERMINAL TAB/WINDOW or run `. /home/jsp/.bashrc`
             before using n and Node.js.
===

途中、プロンプトに 1 回応えるだけでインストールは完了です。インストール後、.bashrc が更新されるのでこれを適用すれば準備完了です(もしくは再ログインなど)。

$ . /home/jsp/.bashrc

インストールは ~/n ディレクトリに行われ、最新 バージョンの node/npm がインストールされます。

$ node -v
v14.15.0
$ npm -v
6.14.8

さらに n の他に、u-update n-uninstall という 2 つのコマンドも使えるようになります。

n-update

n 自身を最新バージョンに更新するコマンドです。

$ n-update
Check for updates to n (version 6.7.0, installed in '/home/jsp/n/n')? (y/N) y
-- Checking for updates...
(Installed version 6.7.0 is up-to-date.)

u-uninstall

n でインストールした node/npm、および n 自身をアンインストールするコマンドです。

$ n-uninstall
====
You are ABOUT TO UNINSTALL n, the Node.js version manager,
which includes REMOVAL of the following directory:

  /home/jsp/n

For more information, see https://git.io/n-install-repo
====
CONTINUE (y/N)? y
-- n successfully uninstalled from '/home/jsp/n'.

n の使い方

n は node/npm のバージョン管理ツールです。簡単な使い方を記します。

バージョンの追加(インストール)

n <バージョン> で指定した node/npm をインストールできます。ltslatest といった指定もできます。

  • lts: 公式の最新 LTS バージョン
  • latest, current: 公式の最新バージョン
$ n 10.16.0
$ n lts

n でインストール済みのバージョンを選択できます。

$ n

  node/4.9.1
ο node/8.11.3
  node/10.15.0

Use up/down arrow keys to select a version, return key to install, d to delete, q to quit

バージョンの削除

n <バージョン ...> で特定のバージョンを削除します。

$ n rm 0.9.4 v0.10.0

n pruneで現在使用中のバージョン以外を全て削除します。

$ n prune