Node.js
![]() | |
作者 | ライアン・ダール |
---|---|
開発元 | Node.js Developers |
初版 | 2009年 |
最新版 |
23.10.0[1] ![]() |
リポジトリ | |
プログラミング 言語 | C++, JavaScript |
対応OS | macOS, Linux, Solaris, FreeBSD, OpenBSD, Windows, webOS |
プラットフォーム | x86, x64, ARM, Power, z/Architecture |
種別 | イベント駆動型 |
ライセンス | MIT License |
公式サイト | nodejs.org |
Node.js(ノード・ジェイエス) はV8 JavaScriptエンジン上に構築されたJavaScript実行環境の1つである[2]。イベント化された入出力を扱うサーバサイドJavaScript環境であり、Webサーバなどのスケーラブルなネットワークプログラムの記述を意図している[3]。ライアン・ダールによって2009年に作成され、ダールを雇用しているJoyentの支援により成長している[4] [5]。
概要
V8 JavaScriptエンジンで動作するが、ChakraCoreバージョンやMozillaによるSpiderMonkey移植のプロジェクトも存在する。
Node.jsはPythonのTwisted、PerlのPerl Object Environment、C言語のlibevent、RubyのEventMachineと同様の目的を持つ。 ほとんどのJavaScriptとは異なり、ウェブブラウザの中で実行されるのではなく、むしろサーバサイドJavaScriptの一種である。 Node.jsはいくつかのCommonJS仕様を実装している[6]。 Node.jsは対話的なテスト用にREPL (Read-eval-print loop) 環境を含んでいる。
Node.jsを用いた構成としてはMEAN等が提唱されている。
例
Node.jsによるHTTPサーバ版のHello world:
const http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
ポート番号 3000番で接続を待ち受けて、送信されたデータをエコーする単純なTCPサーバ:
const net = require('net');
const server = net.createServer(function (stream) {
stream.write('hello\r\n');
stream.on('data', function (data) {
stream.write(data);
});
stream.on('end', function () {
stream.end('goodbye\r\n');
});
});
server.listen(3000, 'localhost');
モジュール
Node.jsは、バイナリにコンパイルされた多くの「コア・モジュール」とともに提供される。それはネットワークの非同期ラッパーであるnetモジュールの他、パスやファイルシステム、バッファ、タイマー、より一般的なストリームなどの基本的なモジュールを含む。サードパーティー製のモジュールを使用することも可能である。それはプリコンパイルされた ".node" アドオン、または、プレーンなJavaScriptファイルのどちらの形式でもよい。JavaScriptモジュールはCommonJSモジュール仕様[7]に従って実装され、モジュールが実装する関数や変数へのアクセスにはexports変数が使われる[8]。
サードパーティーのモジュールはNode.jsを拡張または抽象レベルを提供することで、ウェブアプリケーションで使われる様々なミドルウェア実装することができる。たとえばポピュラーなフレームワークとしてconnectおよびExpress.jsがある。モジュールは単なるファイルとしてインストールすることもできるが、通常はnpmを使ってインストールされる。それは依存性の扱いも含めてモジュールの構築、インストール、更新を助けてくれる。さらに、モジュールはNodeのデフォルトであるモジュール用ディレクトリにインストールしなくても、相対的なパス名を要求することで見つけられる。Node.js wikiに利用可能なサードパーティー製のモジュール一覧がある。
Node.jsを用いたWebアプリケーションでは、Express.jsやEmber.js、Matadorのようなフレームワークを用いて開発を行うことが多い。
表明
Node.jsはコアモジュール assert
により表明(assertion)に対応している。表明違反時にはassert.AssertionError
インスタンスがスローされる[9]。
const assert = require('assert').strict;
assert.deepEqual(1, 2);
// Thrown:
// AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
//
// 1 !== 2
リリース
Node.jsは長期サポート (LTS) リリースモデルを採用している。
各メジャーバージョンはPending -> Current -> (Active LTS -> Maintenance LTS ->) End of Life のリリース状態を経る。Currentリリースは6ヶ月間続き、奇数バージョンはその後LTSを経ることなくサポートが終了し、偶数バージョンはLTSへ移行する。プロダクションアプリケーションは Active LTS あるいは Maintenance LTS のいずれかのみを利用しなければならない[10]。
Release | Status | Code name | Release date | Active LTS start | Maintenance start | Maintenance end |
---|---|---|---|---|---|---|
v0.10.x | End-of-Life | 2013-03-11 | - | 2015-10-01 | 2016-10-31 | |
v0.12.x | End-of-Life | 2015-02-06 | - | 2016-04-01 | 2016-12-31 | |
4.x | End-of-Life | Argon | 2015-09-08 | 2015-10-01 | 2017-04-01 | 2018-04-30 |
5.x | End-of-Life | 2015-10-29 | N/A | 2016-06-30 | ||
6.x | End-of-Life | Boron | 2016-04-26 | 2016-10-18 | 2018-04-30 | 2019-04-30 |
7.x | End-of-Life | 2016-10-25 | N/A | 2017-06-30 | ||
8.x | End-of-Life | Carbon | 2017-05-30 | 2017-10-31 | 2019-01-01[11] | 2019-12-31 |
9.x | End-of-Life | 2017-10-01 | N/A | 2018-06-30 | ||
10.x | Maintenance LTS | Dubnium | 2018-04-24 | 2018-10-30 | 2020-05-19 | 2021-04-01 |
11.x | End-of-Life | 2018-10-23 | N/A | 2019-06-01 | ||
12.x | Active LTS | Erbium | 2019-04-23 | 2019-10-22 | 2020-10-20 | 2022-04-01 |
13.x | End-of-Life | 2019-10-22 | N/A | 2020-04-01 | 2020-06-01 | |
14.x | Active LTS | Fermium | 2020-04-21 | 2020-10-20 | 2021-10-20 | 2023-04-30 |
15.x | Current | 2020-10-20 | N/A | 2021-04-01 | 2021-06-01 | |
16.x | Pending | Gallium | 2021-04-20 | 2021-10-26 | 2022-10-18 | 2024-04-30 |
凡例 サポート終了 サポート中 現行バージョン 将来のリリース |
コミュニティ
主に2つのメーリングリスト nodejs と nodejs-dev 、そして freenode上の IRC チャンネル #node.js を中心とするとても活発な開発者コミュニティが存在する。コミュニティはNode.jsにフォーカスした開発者会議であるNodeConfに集結する[12]。
Windows版
0.5.1より、Windowsネイティブ版バイナリをリリースをした[13]。Windowsネイティブ版リリースに関してはマイクロソフトの支援が行われた。Windows向けの非同期I/O環境(Input/output completion port、以下IOCP)に対応するため、libuv[14]を作成することにより抽象化を進めた。結果としてlibev、libeioが使えるUnix系プラットフォームとIOCPを利用するWindowsプラットフォーム向けのリリースが可能になった。
クライアント1万台問題
非同期処理のNode.jsではクライアント1万台問題は起きない[15]。
Node.jsでこの問題を解決した技術の中核は、シングルスレッドにおける非同期処理を容易に実装可能にしたイベント駆動型プログラミング環境である。
Docker Image
Node.js公式からDockerイメージが配布されている[16]。イメージは以下の3種類に大別され、全てのイメージでnode
/npm
/yarn
がプリインストールされている[17]。
node:<version>
: デファクトスタンダード[18]。Docker公式のbuildpack-deps
を基に構築[19]node:alpine
: Alpine Linuxベース[20]。イメージサイズを最小化したい際に推奨される[21]node:slim
: Node.js動作に必要最低限のパッケージのみを含む[22]。サイズに制限がない限りデファクトイメージの利用を強く推奨[23]
デフォルト以外の設定(例: npmアップグレード)を利用する手引き「Docker and Node.js Best Practices」や GetStarted が公式から提供されている。
脚注・出典
- ^ "Release 23.10.0"; 閲覧日: 2025年3月27日; 出版日: 2025年3月13日.
- ^ Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js
- ^ http://www.readwriteweb.com/hack/2011/01/wait-whats-nodejs-good-for-aga.php
- ^ http://mashable.com/2011/03/10/node-js/
- ^ Alex Handy (2011年6月24日). “Node.js pushes JavaScript to the server-side”. SDTimes. 2011年6月24日閲覧。
- ^ http://wiki.commonjs.org/wiki/Implementations/node.js
- ^ “CommonJS Implementations”. 2011年5月15日閲覧。
- ^ Ryswyck, Jan. “Taking Baby Steps with Node.js – CommonJS and Creating Custom Modules”. 2011年5月15日閲覧。
- ^ All errors thrown by the
assert
module will be instances of theAssertionError
class. Node.js - ^ Production applications should only use Active LTS or Maintenance LTS releases. Node.js
- ^ “Node 8 reschedule”. 2019年1月22日閲覧。
- ^ http://www.readwriteweb.com/hack/2011/04/nodeconf-schedule-announced.php
- ^ http://blog.nodejs.org/2011/07/14/node-v0-5-1/
- ^ https://github.com/joyent/libuv
- ^ 福田崇男 (2012年7月31日). “スタバ方式で「C10K問題」を解消”. 日経コンピュータ. 日経BP社. 2017年2月20日閲覧。
- ^ The official Node.js docker image, made with love by the node community. [1]
- ^ All of the images contain pre-installed versions of
node
,npm
, andyarn
. [2] - ^ This is the defacto image. [3]
- ^ This tag is based off of
buildpack-deps
. [4] - ^ This image is based on the popular Alpine Linux project, available in the
alpine
official image. [5] - ^ This variant is highly recommended when final image size being as small as possible is desired. [6]
- ^ This image does not contain the common packages contained in the default tag and only contains the minimal packages needed to run
node
. [7] - ^ Unless you are working in an environment where only the Node.js image will be deployed and you have space constraints, we highly recommend using the default image of this repository. [8]