Node.js(ノード・ジェイエス) はV8 JavaScriptエンジン上に構築されたJavaScript実行環境の1つであるNode.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js。イベント化された入出力を扱うサーバサイドJavaScript環境であり、Webサーバなどのスケーラブルなネットワークプログラムの記述を意図しているhttps://readwrite.com/wait-whats-nodejs-good-for-aga/。ライアン・ダールによって2009年に作成され、ダールを雇用しているJoyentの支援により成長しているhttps://mashable.com/archive/node-js 。
概要 V8 JavaScriptエンジンで動作するが、ChakraCoreバージョンやMozillaによるSpiderMonkey移植のプロジェクトも存在する。
Node.jsはPythonのTwisted、Perlの、C言語の、Rubyのと同様の目的を持つ。 ほとんどのJavaScriptとは異なり、ウェブブラウザの中で実行されるのではなく、むしろサーバサイドJavaScriptの一種である。 Node.jsはいくつかのCommonJS仕様を実装しているhttp://wiki.commonjs.org/wiki/Implementations/node.js。 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モジュール仕様に従って実装され、モジュールが実装する関数や変数へのアクセスにはexports変数が使われる。
サードパーティーのモジュールはNode.jsを拡張または抽象レベルを提供することで、ウェブアプリケーションで使われる様々なミドルウェア実装することができる。たとえばポピュラーなフレームワークとしてconnectおよびExpress.jsがある。モジュールは単なるファイルとしてインストールすることもできるが、通常はnpmを使ってインストールされる。それは依存性の扱いも含めてモジュールの構築、インストール、更新を助けてくれる。さらに、モジュールはNodeのデフォルトであるモジュール用ディレクトリにインストールしなくても、相対的なパス名を要求することで見つけられる。Node.js wikiに利用可能なサードパーティー製のモジュール一覧がある。
Node.jsを用いたWebアプリケーションでは、Express.jsや、のようなフレームワークを用いて開発を行うことが多い。
表明 Node.jsはコアモジュール assertにより表明(assertion)に対応している。表明違反時にはassert.AssertionErrorインスタンスがスローされるAll errors thrown by the assert module will be instances of the AssertionError class. Node.js。const assert = require('assert').strict;
assert.deepEqual(1, 2); // Thrown: // AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: // // 1 !== 2
ECMAScript modules Node.jsは ECMAScript modules (ES module) に対応しており"Node.js fully supports ECMAScript modules" Modules: ECMAScript modules. Node.js v16.19.1 documentation. 2023-02-19閲覧.、以下のいずれかを満たすものをES moduleとしてロードする"Node.js will treat the following as ES modules ... Files with an .mjs extension ... Files with a .js extension when the nearest parent package.json file contains a top-level "type" field with a value of "module". ... as an argument ... with the flag --input-type=module." Modules: Packages. Node.js v16.19.1 documentation. 2023-02-19閲覧.。
拡張子が .mjs であるファイル 拡張子が .js かつ最近傍の親 package.json で "type":"module" が指定されたファイル --input-type=module フラグと共に引数として渡されたモジュール文字列
リリース Node.jsは長期サポート (LTS) リリースモデルを採用している。
各メジャーバージョンはPending → Current → (Active LTS → Maintenance LTS →) End of Life のリリース状態を経る。Currentリリースは6ヶ月間続き、奇数バージョンはその後Maintenance LTSのみを経てサポートが終了し、偶数バージョンはActive LTSを経てMaintenance LTSへ移行する。プロダクションアプリケーションは Active LTS あるいは Maintenance LTS のいずれかのみを利用しなければならないProduction applications should only use Active LTS or Maintenance LTS releases. Node.js。
ReleaseStatusCode nameRelease dateActive LTS startMaintenance startMaintenance end 2013-03-11 - 2015-10-01 2016-10-31 2015-02-06 - 2016-04-01 2016-12-31 Argon 2015-09-08 2015-10-01 2017-04-01 2018-04-30 2015-10-29N/A 2016-06-30 Boron 2016-04-26 2016-10-18 2018-04-30 2019-04-30 2016-10-25N/A 2017-06-30 Carbon 2017-05-30 2017-10-31 2019-01-01 2019-12-31 2017-10-01N/A 2018-06-30 Dubnium 2018-04-24 2018-10-30 2020-05-19 2021-04-01 2018-10-23 N/A 2019-05-01 2019-06-01 Erbium 2019-04-23 2019-10-21 2020-11-30 2022-04-30 2019-10-22 N/A 2020-04-01 2020-06-01 Fermium 2020-04-21 2020-10-27 2021-10-19 2023-04-30 2020-10-20 N/A 2021-04-01 2021-06-01 Gallium 2021-04-20 2021-10-26 2022-10-18 2023-09-11 2021-10-19 N/A 2022-04-01 2022-06-01 Hydrogen 2022-04-19 2022-10-25 2023-10-18 2025-04-30 2022-10-18 N/A 2023-04-01 2023-06-01 Iron 2023-04-18 2023-10-24 2024-10-22 2026-04-30 2023-10-17 N/A 2024-04-01 2024-06-01 Jod 2024-04-24 2024-10-29 2025-10-21 2027-04-30 2024-10-16 N/A 2025-04-01 2025-06-01 2025-05-06 2025-10-28 2026-10-20 2028-04-30
コミュニティ 主に2つのメーリングリスト nodejs と nodejs-dev 、そして freenode上の IRC チャンネル #node.js を中心とするとても活発な開発者コミュニティが存在する。コミュニティはNode.jsにフォーカスした開発者会議であるNodeConfに集結するhttps://readwrite.com/nodeconf-schedule-announced/。
Windows版 0.5.1より、Windowsネイティブ版バイナリをリリースをしたhttp://blog.nodejs.org/2011/07/14/node-v0-5-1/。Windowsネイティブ版リリースに関してはマイクロソフトの支援が行われた。Windows向けの非同期I/O環境(、以下IOCP)に対応するため、libuvhttps://github.com/joyent/libuvを作成することにより抽象化を進めた。結果としてlibev、libeioが使えるUnix系プラットフォームとIOCPを利用するWindowsプラットフォーム向けのリリースが可能になった。
クライアント1万台問題
非同期処理のNode.jsではクライアント1万台問題は起きない。
Node.jsでこの問題を解決した技術の中核は、シングルスレッドにおける非同期処理を容易に実装可能にしたイベント駆動型プログラミング環境である。
Docker Image Node.js公式からDockerイメージが配布されているThe official Node.js docker image, made with love by the node community. 。イメージは以下の3種類に大別され、全てのイメージでnode/npm/yarnがプリインストールされているAll of the images contain pre-installed versions of node, npm, and yarn. 。
node:<version>: デファクトスタンダードThis is the defacto image. 。Docker公式のbuildpack-depsを基に構築This tag is based off of buildpack-deps. node:alpine: Alpine LinuxベースThis image is based on the popular Alpine Linux project, available in the alpine official image. 。イメージサイズを最小化したい際に推奨されるThis variant is highly recommended when final image size being as small as possible is desired. node:slim: Node.js動作に必要最低限のパッケージのみを含むThis image does not contain the common packages contained in the default tag and only contains the minimal packages needed to run node. 。サイズに制限がない限りデファクトイメージの利用を強く推奨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.
デフォルト以外の設定(例: npmアップグレード)を利用する手引き「Docker and Node.js Best Practices」や GetStarted が公式から提供されている。
脚注・出典
関連項目
JavaScript V8 Deno MEAN (ソフトウェアバンドル) Bun (ソフトウェア)
外部リンク
Node.js 日本ユーザグループ