javascriptの標準モジュールです。
これから作成するにはこちらを使ったほうがいいかと思います。
Modules: ECMAScript modules
Introduction
はじめに
ECMAScript modules are the official standard format to package JavaScript code for reuse. Modules are defined using a variety of import and export statements.
ECMAScriptモジュールは、再利用のためにJavaScriptコードをパッケージ化するための公式な標準フォーマットである。モジュールは、さまざまなインポート文とエクスポート文を使用して定義される。
The following example of an ES module exports a function:
次のESモジュールの例では、関数をエクスポートする。
// addTwo.mjs
function addTwo(num) {
return num + 2;
}
export { addTwo };
The following example of an ES module imports the function from addTwo.mjs:
次のESモジュールの例では、addTwo.mjsから関数をインポートする。
// app.mjs
import { addTwo } from './addTwo.mjs';
// Prints: 6
console.log(addTwo(4));
Node.js fully supports ECMAScript modules as they are currently specified and provides interoperability between them and its original module format, CommonJS.
Node.jsは、現在指定されているECMAScriptモジュールを完全にサポートし、それらと元のモジュールフォーマットであるCommonJSとの間の相互運用性を提供する。
Enabling
Node.js has two module systems: CommonJS modules and ECMAScript modules.
Node.jsには、CommonJSモジュールとECMAScriptモジュールの2つのモジュールシステムがある。
Authors can tell Node.js to use the ECMAScript modules loader via the .mjs file extension, the package.json "type" field, or the --input-type flag. Outside of those cases, Node.js will use the CommonJS module loader. See Determining module system for more details.
作成者は、.mjsファイル拡張子、package.jsonの「type」フィールド、または--input-typeフラグを介して、ECMAScriptモジュールローダーを使用するようにNode.jsに指示することができる。
これらの場合以外では、Node.jsはCommonJSモジュールローダーを使用する。詳細については、モジュールシステムの決定を参照してください。
Packages
This section was moved to Modules: Packages.
このセクションは「モジュール:パッケージ」に移動されました。
import Specifiers
インポート指定子
The specifier of an import statement is the string after the from keyword, e.g. 'node:path' in import { sep } from 'node:path'. Specifiers are also used in export from statements, and as the argument to an import() expression.
インポート文の指定子は、fromキーワードの後の文字列です。
たとえば、import{sep}from'node:path'の'node:path'です。指定子は、export from文やimport()式の引数としても使用されます。
There are three types of specifiers:
指定子には、次の3種類があります。
・Relative specifiers like './startup.js' or '../config.mjs'. They refer to a path relative to the location of the importing file. The file extension is always necessary for these.
'./startup.js'や'../config.mjs'などの相対指定子。
これらはインポートするファイルの場所に対する相対パスを参照します。
これらにはファイル拡張子が常に必要です。
・Bare specifiers like 'some-package' or 'some-package/shuffle'. They can refer to the main entry point of a package by the package name, or a specific feature module within a package prefixed by the package name as per the examples respectively. Including the file extension is only necessary for packages without an "exports" field.
「some-package」や「some-package/shuffle」のような裸の指定子。
これらは、パッケージ名でパッケージのメインエントリポイントを参照することも、それぞれの例のようにパッケージ名でプレフィックスされたパッケージ内の特定のフィーチャモジュールを参照することもできます。
ファイル拡張子を含める必要があるのは、「exports」フィールドのないパッケージの場合だけです。
・Absolute specifiers like 'file:///opt/nodejs/config.js'. They refer directly and explicitly to a full path.
「file:///opt/nodejs/config.js」のような絶対指定子。
これらは、フルパスを直接かつ明示的に参照する。
Bare specifier resolutions are handled by the Node.js module resolution and loading algorithm. All other specifier resolutions are always only resolved with the standard relative URL resolution semantics.
裸の指定子の解決は、Node.jsモジュールの解決とロードアルゴリズムによって処理される。
その他のすべての指定子の解決は、常に標準の相対URL解決セマンティクスでのみ解決される。
Like in CommonJS, module files within packages can be accessed by appending a path to the package name unless the package's package.json contains an "exports" field, in which case files within packages can only be accessed via the paths defined in "exports".
CommonJSと同様に、パッケージ内のモジュールファイルは、パッケージのpackage.jsonに「exports」フィールドが含まれていない限り、パッケージ名にパスを追加することでアクセスできる。
この場合、パッケージ内のファイルは、「exports」で定義されたパスを介してのみアクセスできる。
For details on these package resolution rules that apply to bare specifiers in the Node.js module resolution, see the packages documentation.
Node.jsモジュール解決のベア指定子に適用されるこれらのパッケージ解決ルールの詳細については、パッケージのドキュメントを参照してください。
Mandatory file extensions
必須ファイル拡張子
A file extension must be provided when using the import keyword to resolve relative or absolute specifiers. Directory indexes (e.g. './startup/index.js') must also be fully specified.
importキーワードを使用して相対指定子または絶対指定子を解決する場合は、ファイル拡張子を指定する必要がある。
ディレクトリインデックス(例:「./startup/index.js」)も完全に指定する必要がある。
This behavior matches how import behaves in browser environments, assuming a typically configured server.
この動作は、一般的に設定されたサーバを想定した場合、ブラウザ環境でのインポートの動作と同じです。
URLs
ES modules are resolved and cached as URLs. This means that special characters must be percent-encoded, such as # with %23 and ? with %3F.
ESモジュールは解決され、URLとしてキャッシュされます。つまり、#with%23や?with%3Fなどの特殊文字はパーセントでエンコードする必要があります。
file:, node:, and data: URL schemes are supported. A specifier like 'https://example.com/app.js' is not supported natively in Node.js unless using a custom HTTPS loader.
file:、node:、data:のURLスキームがサポートされている。
'https://example.com/app.js'のような指定子は、カスタムHTTPSローダーを使用しない限り、Node.jsではネイティブにサポートされていない。
file: URLs
Modules are loaded multiple times if the import specifier used to resolve them has a different query or fragment.
モジュールを解決するために使用されるインポート指定子が異なるクエリまたはフラグメントを持つ場合、モジュールは複数回ロードされる。
import './foo.mjs?query=1'; // loads ./foo.mjs with query of "?query=1" "?query=1"のクエリで./foo.mjsをロードします
import './foo.mjs?query=2'; // loads ./foo.mjs with query of "?query=2" "?query=2"の照会で./foo.mjsをロードします
The volume root may be referenced via /, //, or file:///. Given the differences between URL and path resolution (such as percent encoding details), it is recommended to use url.pathToFileURL when importing a path.
ボリュームルートは、/、//、またはfile:///を介して参照できる。
URLとパスの解決(パーセントエンコーディングの詳細など)の違いを考慮すると、パスをインポートするときにはurl.pathToFileURLを使用することをお勧めする。
data: imports
data: URLs are supported for importing with the following MIME types:
データ:URLは、次のMIMEタイプでのインポートがサポートされています。
・text/javascript for ES modules ES モジュール
・application/json for JSON JSON
・application/wasm for Wasm Wasm
import 'data:text/javascript,console.log("hello!");';
import _ from 'data:application/json,"world!"' assert { type: 'json' };
data: URLs only resolve bare specifiers for builtin modules and absolute specifiers. Resolving relative specifiers does not work because data: is not a special scheme. For example, attempting to load ./foo from data:text/javascript,import "./foo"; fails to resolve because there is no concept of relative resolution for data: URLs.
data:URLは、組込みモジュールおよび絶対指定子のベア指定子のみを解決します。
data:は特別なスキームではないため、相対指定子の解決は機能しません。
たとえば、data:text/javascriptから./fooをロードしようとすると、import"./foo";は、data:URLの相対解決の概念がないため、解決に失敗します。
node: imports
node: URLs are supported as an alternative means to load Node.js builtin modules. This URL scheme allows for builtin modules to be referenced by valid absolute URL strings.
node:URLは、Node.js組み込みモジュールをロードするための代替手段としてサポートされています。
このURLスキームにより、組み込みモジュールを有効な絶対URL文字列で参照できます。
import fs from 'node:fs/promises';
Import assertions
Stability: 1 - Experimental // 安定性:1-実験的
The Import Assertions proposal adds an inline syntax for module import statements to pass on more information alongside the module specifier.
Import Assertionsの提案では、モジュール指定子とともにより多くの情報を渡すために、モジュールインポート文のインライン構文が追加されている。
import fooData from './foo.json' assert { type: 'json' };
const { default: barData } =
await import('./bar.json', { assert: { type: 'json' } });
Node.js supports the following type values, for which the assertion is mandatory:
Node.jsは、アサーションが必須である次の型の値をサポートしている。
Assertion type Needed for
'json' JSON modules
次回は、
Builtin modules
からです。