javascript:nodejs

Node.js - サーバーサイド JavaScript 環境

日本ユーザグループ: Japan Node.js Association
本家: Node.js

Node.js はV8 JavaScript エンジン上に構築された JavaScript 実行環境の 1 つである。イベント化された入出力を扱うサーバーサイド JavaScript 環境であり、Web サーバなどのスケーラブルなネットワークプログラムの記述を意図している。ライアン・ダールによって 2009 年に作成され、ダールを雇用している Joyent の支援により成長している。
Node.js - Wikipedia より

詳しくは anyenv - rbenv スタイルのオールインワン環境マネージャー を参照。

anyenv-update プラグインで nodenvnode-build をコマンド一発で最新化する😉

$ anyenv update nodenv

Updating 'nodenv'...
Updating 'nodenv/node-build'...
 |  From https://github.com/nodenv/node-build
 |  * [new branch]        latest-scraped-definitions -> origin/latest-scraped-definitions
Updating 'nodenv/nodenv-vars'...
Updating 'anyenv manifest directory'...

node のインストール…🤔

$ nodenv install 18.14.0

Downloading node-v18.14.0-linux-x64.tar.gz...
-> https://nodejs.org/dist/v18.14.0/node-v18.14.0-linux-x64.tar.gz
Installing node-v18.14.0-linux-x64...
Installed node-v18.14.0-linux-x64 to /home/tomoyan/.anyenv/envs/nodenv/versions/18.14.0

$ nodenv rehash
$ nodenv global 18.14.0
$ nodenv versions

* 18.14.0 (set by /home/tomoyan/.anyenv/envs/nodenv/version)

$ node -v

v18.14.0

$ npm -v

9.3.1

$ npx -v

9.3.1

$ npm -g list

/home/tomoyan/.anyenv/envs/nodenv/versions/18.14.0/lib
├── corepack@0.15.3
└── npm@9.3.1

本家: nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions

Node Version Manager(nvm) のインストールを行う。

$ curl -kL git.io/nodebrew | perl - setup

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
  0     0    0     0    0     0      0      0 --:--:--  0:00:03 --:--:--     0
100 24634  100 24634    0     0   5131      0  0:00:04  0:00:04 --:--:-- 30041
Fetching nodebrew...
Installed nodebrew in $HOME/.nodebrew

========================================
Export a path to nodebrew:

export PATH=$HOME/.nodebrew/current/bin:$PATH
========================================

指示にしたがってパスを設定する。

$ vi ~/.bashrc

# User specific aliases and functions
export PATH=$HOME/.nodebrew/current/bin:$PATH

$ source ~/.bashrc

インストール可能なバージョンを調べる。

$ nodebrew ls-remote

〜省略〜
v12.7.0   v12.8.0   v12.8.1   v12.9.0   v12.9.1   v12.10.0    
〜省略〜

バージョンを指定してバイナリインストールする。

$ nodebrew install-binary v12.10.0

Fetching: https://nodejs.org/dist/v12.10.0/node-v12.10.0-linux-x64.tar.gz
######################################################################### 100.0%
Installed successfully

ビルドに必要なパッケージをインストールする。

$ sudo dnf install openssl-devel gcc-c++

nodebrew を使って node.js の最新バージョンをビルドしてインストールする場合。

$ nodebrew install latest

fetch: http://nodejs.org/dist/v5.4.1/node-v5.4.1.tar.gz
######################################################################## 100.0%
〜省略〜
installing /home/tomoyan/.nodebrew/node/v5.4.1/bin/node
installing /home/tomoyan/.nodebrew/node/v5.4.1/share/systemtap/tapset/node.stp
installing /home/tomoyan/.nodebrew/node/v5.4.1/share/doc/node/gdbinit
〜省略〜

インストールされているバージョンを確認する。

$ nodebrew ls

v12.10.0

current: none

利用するバージョンを指定します。

$ nodebrew use v12.10.0

use v12.10.0

アンインストールする場合。

$ nodebrew uninstall v7.7.2

v7.7.2 uninstalled

動作確認を行います。

$ nodebrew use v12.10.0

use v12.10.0

$ node -v

v12.10.0

ソースコード: nvm-windows

Scoop によるインストール。

nvm (Node Version Manager) をインストールする。
※ここでインストールされる nvmnvm-windows である。Linux の nvm とはオプションなどが異なる。

$ scoop install nvm

Installing 'nvm' (1.1.9) [64bit]
nvm-noinstall.zip (3.6 MB) [==================================================================================] 100%
Checking hash of nvm-noinstall.zip ... ok.
Extracting nvm-noinstall.zip ... done.
Running pre_install script...
Linking ~\scoop\apps\nvm\current => ~\scoop\apps\nvm\1.1.9
Creating shim for 'nvm'.
Persisting nodejs
Persisting elevate.cmd
Persisting elevate.vbs
Persisting settings.txt
'nvm' (1.1.9) was installed successfully!
Notes
-----
You'll need to restart powershell/cmd to have it reload Environment Variables so nvm will work correctly

$ nvm version

1.1.9

※Windows の nvm では、バージョン確認は version オプションである。

$ nvm


Running version 1.1.9.

Usage:

  nvm arch                     : Show if node is running in 32 or 64 bit mode.
  nvm current                  : Display active version.
  nvm install  [arch] : The version can be a specific version, "latest" for the latest current version, or "lts" for the
                                 most recent LTS version. Optionally specify whether to install the 32 or 64 bit version (defaults
                                 to system arch). Set [arch] to "all" to install 32 AND 64 bit versions.
                                 Add --insecure to the end of this command to bypass SSL validation of the remote download server.
  nvm list [available]         : List the node.js installations. Type "available" at the end to see what can be installed. Aliased as ls.
  nvm on                       : Enable node.js version management.
  nvm off                      : Disable node.js version management.
  nvm proxy [url]              : Set a proxy to use for downloads. Leave [url] blank to see the current proxy.
                                 Set [url] to "none" to remove the proxy.
  nvm node_mirror [url]        : Set the node mirror. Defaults to https://nodejs.org/dist/. Leave [url] blank to use default url.
  nvm npm_mirror [url]         : Set the npm mirror. Defaults to https://github.com/npm/cli/archive/. Leave [url] blank to default url.
  nvm uninstall       : The version must be a specific version.
  nvm use [version] [arch]     : Switch to use the specified version. Optionally use "latest", "lts", or "newest".
                                 "newest" is the latest installed version. Optionally specify 32/64bit architecture.
                                 nvm use  will continue using the selected version, but switch to 32/64 bit mode.
  nvm root [path]              : Set the directory where nvm should store different versions of node.js.
                                 If  is not set, the current root will be displayed.
  nvm version                  : Displays the current running version of nvm for Windows. Aliased as v.

$ nvm list available


|   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
|    18.6.0    |   16.16.0    |   0.12.18    |   0.11.16    |
|    18.5.0    |   16.15.1    |   0.12.17    |   0.11.15    |
|    18.4.0    |   16.15.0    |   0.12.16    |   0.11.14    |
|    18.3.0    |   16.14.2    |   0.12.15    |   0.11.13    |
|    18.2.0    |   16.14.1    |   0.12.14    |   0.11.12    |
|    18.1.0    |   16.14.0    |   0.12.13    |   0.11.11    |
|    18.0.0    |   16.13.2    |   0.12.12    |   0.11.10    |
|    17.9.1    |   16.13.1    |   0.12.11    |    0.11.9    |
|    17.9.0    |   16.13.0    |   0.12.10    |    0.11.8    |
|    17.8.0    |   14.20.0    |    0.12.9    |    0.11.7    |
|    17.7.2    |   14.19.3    |    0.12.8    |    0.11.6    |
|    17.7.1    |   14.19.2    |    0.12.7    |    0.11.5    |
|    17.7.0    |   14.19.1    |    0.12.6    |    0.11.4    |
|    17.6.0    |   14.19.0    |    0.12.5    |    0.11.3    |
|    17.5.0    |   14.18.3    |    0.12.4    |    0.11.2    |
|    17.4.0    |   14.18.2    |    0.12.3    |    0.11.1    |
|    17.3.1    |   14.18.1    |    0.12.2    |    0.11.0    |
|    17.3.0    |   14.18.0    |    0.12.1    |    0.9.12    |
|    17.2.0    |   14.17.6    |    0.12.0    |    0.9.11    |
|    17.1.0    |   14.17.5    |   0.10.48    |    0.9.10    |

This is a partial list. For a complete list, visit https://nodejs.org/en/download/releases

nvm install <version> [arch] arch=32 or 64 or all

$ nvm install 18.18.0 64

Downloading node.js version 18.18.0 (64-bit)...
Extracting node and npm...
Complete
npm v9.8.1 installed successfully.


Installation complete. If you want to use this version, type

nvm use 18.18.0

$ nvm use 18.18.0 64

Now using node v18.18.0 (64-bit)

$ node --version

v18.18.0

特に理由がない場合は Node.js の最新の LTS を使うべきです🤔
以下は古い 16.17.0 LTS を使っていて発生しました😅

$ npm install -g firebase-tools

npm WARN deprecated har-validator@5.1.3: this library is no longer supported
npm WARN deprecated debug@4.1.1: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142

added 701 packages, and audited 702 packages in 18s

44 packages are looking for funding
  run `npm fund` for details

1 high severity vulnerability

To address all issues, run:
  npm audit fix

Run `npm audit` for details.

How to npm audit global packages - Stack Overflow より…

公式のnpm ドキュメント npm auditに記載されているように、ローカルにインストールされたパッケージで手動で実行できます。これには、package.jsonpackage-lock.json ファイルの両方が必要です。
グローバルにインストールされたパッケージには package-lock.json がありません。監査を実行すると、エラーが発生します。

$ npm audit -g

npm ERR! code EAUDITGLOBAL
npm ERR! `npm audit` does not support testing globals

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/tomoyan/.npm/_logs/2023-02-08T20_15_56_893Z-debug-0.log

グローバル パッケージを ncu (npm-check-updates) で調べる方法

$ npm install -g npm-check-updates

npm WARN deprecated @npmcli/move-file@2.0.1: This functionality has been moved to @npmcli/fs

added 297 packages, and audited 298 packages in 30s

57 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

$ nodenv rehash
$ ncu -g

[====================] 5/5 100%

 corepack  0.12.1  →  0.15.3
 npm       8.15.0  →   9.4.2

ncu itself cannot upgrade global packages. Run the following to upgrade all global packages: 

npm -g install corepack@0.15.3 npm@9.4.2

nodenv rehash を実行すると ~/.anyenv/envs/nodenv/shims/ が生成されて ncu コマンドが使えるようになる🤔

ncu に指示されたパッケージを更新する🤔

$ npm -g install corepack@0.15.3 npm@9.4.2


removed 7 packages, changed 95 packages, and audited 235 packages in 5s

16 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

ncu で確認😀

$ ncu -g

[====================] 5/5 100%

All global packages are up-to-date :)

Node.js パッケージのインストール時に npm notice 通知がされる場合。

$ npm install -g firebase-tools

npm WARN deprecated har-validator@5.1.3: this library is no longer supported
npm WARN deprecated debug@4.1.1: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142

added 703 packages in 24s

48 packages are looking for funding
  run `npm fund` for details
npm notice
npm notice New minor version of npm available! 9.5.0 -> 9.6.4
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.6.4
npm notice Run npm install -g npm@9.6.4 to update!
npm notice

npm notice 通知に従って npm パッケージを更新する🤔

$ npm install -g npm@9.6.4


removed 1 package, and changed 43 packages in 11s

18 packages are looking for funding
  run `npm fund` for details

ERROR open \settings.txt: · Issue #22 · coreybutler/nvm-windows

$ nvm use 14.17.5

ERROR open \settings.txt: The system cannot find the file specified.

環境変数 NVM_HOMENVM_SYMLINK が存在する場合は削除する。
cmdpowershell を完全に再起動するために、Windows Terminal も完全に終了して起動し直す。

exit status 1: ���̑��������s���邽�߂̏\���ȓ����������܂����B が発生する😵

Garbled error message when `nvm use` fails in Japanese environment. · Issue #740 · coreybutler/nvm-windows

$ nvm use 14.17.5

exit status 1: ���̑��������s���邽�߂̏\���ȓ����������܂����B

管理者実行しないと使えない機能がある。

$ sudo nvm use 14.17.5

Now using node v14.17.5 (64-bit)

  • javascript/nodejs.txt
  • 最終更新: 2023/09/26 13:02
  • by ともやん