11cb0ef41Sopenharmony_ci{ 21cb0ef41Sopenharmony_ci "type": "module", 31cb0ef41Sopenharmony_ci "source": "doc/api/module.md", 41cb0ef41Sopenharmony_ci "modules": [ 51cb0ef41Sopenharmony_ci { 61cb0ef41Sopenharmony_ci "textRaw": "Modules: `node:module` API", 71cb0ef41Sopenharmony_ci "name": "modules:_`node:module`_api", 81cb0ef41Sopenharmony_ci "introduced_in": "v12.20.0", 91cb0ef41Sopenharmony_ci "meta": { 101cb0ef41Sopenharmony_ci "added": [ 111cb0ef41Sopenharmony_ci "v0.3.7" 121cb0ef41Sopenharmony_ci ], 131cb0ef41Sopenharmony_ci "changes": [] 141cb0ef41Sopenharmony_ci }, 151cb0ef41Sopenharmony_ci "modules": [ 161cb0ef41Sopenharmony_ci { 171cb0ef41Sopenharmony_ci "textRaw": "The `Module` object", 181cb0ef41Sopenharmony_ci "name": "the_`module`_object", 191cb0ef41Sopenharmony_ci "desc": "<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\"><Object></a></li>\n</ul>\n<p>Provides general utility methods when interacting with instances of\n<code>Module</code>, the <a href=\"modules.html#the-module-object\"><code>module</code></a> variable often seen in <a href=\"modules.html\">CommonJS</a> modules. Accessed\nvia <code>import 'node:module'</code> or <code>require('node:module')</code>.</p>", 201cb0ef41Sopenharmony_ci "properties": [ 211cb0ef41Sopenharmony_ci { 221cb0ef41Sopenharmony_ci "textRaw": "`builtinModules` {string\\[]}", 231cb0ef41Sopenharmony_ci "type": "string\\[]", 241cb0ef41Sopenharmony_ci "name": "builtinModules", 251cb0ef41Sopenharmony_ci "meta": { 261cb0ef41Sopenharmony_ci "added": [ 271cb0ef41Sopenharmony_ci "v9.3.0", 281cb0ef41Sopenharmony_ci "v8.10.0", 291cb0ef41Sopenharmony_ci "v6.13.0" 301cb0ef41Sopenharmony_ci ], 311cb0ef41Sopenharmony_ci "changes": [] 321cb0ef41Sopenharmony_ci }, 331cb0ef41Sopenharmony_ci "desc": "<p>A list of the names of all modules provided by Node.js. Can be used to verify\nif a module is maintained by a third party or not.</p>\n<p><code>module</code> in this context isn't the same object that's provided\nby the <a href=\"modules.html#the-module-wrapper\">module wrapper</a>. To access it, require the <code>Module</code> module:</p>\n<pre><code class=\"language-mjs\">// module.mjs\n// In an ECMAScript module\nimport { builtinModules as builtin } from 'node:module';\n</code></pre>\n<pre><code class=\"language-cjs\">// module.cjs\n// In a CommonJS module\nconst builtin = require('node:module').builtinModules;\n</code></pre>" 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci ], 361cb0ef41Sopenharmony_ci "methods": [ 371cb0ef41Sopenharmony_ci { 381cb0ef41Sopenharmony_ci "textRaw": "`module.createRequire(filename)`", 391cb0ef41Sopenharmony_ci "type": "method", 401cb0ef41Sopenharmony_ci "name": "createRequire", 411cb0ef41Sopenharmony_ci "meta": { 421cb0ef41Sopenharmony_ci "added": [ 431cb0ef41Sopenharmony_ci "v12.2.0" 441cb0ef41Sopenharmony_ci ], 451cb0ef41Sopenharmony_ci "changes": [] 461cb0ef41Sopenharmony_ci }, 471cb0ef41Sopenharmony_ci "signatures": [ 481cb0ef41Sopenharmony_ci { 491cb0ef41Sopenharmony_ci "return": { 501cb0ef41Sopenharmony_ci "textRaw": "Returns: {require} Require function", 511cb0ef41Sopenharmony_ci "name": "return", 521cb0ef41Sopenharmony_ci "type": "require", 531cb0ef41Sopenharmony_ci "desc": "Require function" 541cb0ef41Sopenharmony_ci }, 551cb0ef41Sopenharmony_ci "params": [ 561cb0ef41Sopenharmony_ci { 571cb0ef41Sopenharmony_ci "textRaw": "`filename` {string|URL} Filename to be used to construct the require function. Must be a file URL object, file URL string, or absolute path string.", 581cb0ef41Sopenharmony_ci "name": "filename", 591cb0ef41Sopenharmony_ci "type": "string|URL", 601cb0ef41Sopenharmony_ci "desc": "Filename to be used to construct the require function. Must be a file URL object, file URL string, or absolute path string." 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci ] 631cb0ef41Sopenharmony_ci } 641cb0ef41Sopenharmony_ci ], 651cb0ef41Sopenharmony_ci "desc": "<pre><code class=\"language-mjs\">import { createRequire } from 'node:module';\nconst require = createRequire(import.meta.url);\n\n// sibling-module.js is a CommonJS module.\nconst siblingModule = require('./sibling-module');\n</code></pre>" 661cb0ef41Sopenharmony_ci }, 671cb0ef41Sopenharmony_ci { 681cb0ef41Sopenharmony_ci "textRaw": "`module.isBuiltin(moduleName)`", 691cb0ef41Sopenharmony_ci "type": "method", 701cb0ef41Sopenharmony_ci "name": "isBuiltin", 711cb0ef41Sopenharmony_ci "meta": { 721cb0ef41Sopenharmony_ci "added": [ 731cb0ef41Sopenharmony_ci "v18.6.0" 741cb0ef41Sopenharmony_ci ], 751cb0ef41Sopenharmony_ci "changes": [] 761cb0ef41Sopenharmony_ci }, 771cb0ef41Sopenharmony_ci "signatures": [ 781cb0ef41Sopenharmony_ci { 791cb0ef41Sopenharmony_ci "return": { 801cb0ef41Sopenharmony_ci "textRaw": "Returns: {boolean} returns true if the module is builtin else returns false", 811cb0ef41Sopenharmony_ci "name": "return", 821cb0ef41Sopenharmony_ci "type": "boolean", 831cb0ef41Sopenharmony_ci "desc": "returns true if the module is builtin else returns false" 841cb0ef41Sopenharmony_ci }, 851cb0ef41Sopenharmony_ci "params": [ 861cb0ef41Sopenharmony_ci { 871cb0ef41Sopenharmony_ci "textRaw": "`moduleName` {string} name of the module", 881cb0ef41Sopenharmony_ci "name": "moduleName", 891cb0ef41Sopenharmony_ci "type": "string", 901cb0ef41Sopenharmony_ci "desc": "name of the module" 911cb0ef41Sopenharmony_ci } 921cb0ef41Sopenharmony_ci ] 931cb0ef41Sopenharmony_ci } 941cb0ef41Sopenharmony_ci ], 951cb0ef41Sopenharmony_ci "desc": "<pre><code class=\"language-mjs\">import { isBuiltin } from 'node:module';\nisBuiltin('node:fs'); // true\nisBuiltin('fs'); // true\nisBuiltin('wss'); // false\n</code></pre>" 961cb0ef41Sopenharmony_ci }, 971cb0ef41Sopenharmony_ci { 981cb0ef41Sopenharmony_ci "textRaw": "`module.register(specifier[, parentURL][, options])`", 991cb0ef41Sopenharmony_ci "type": "method", 1001cb0ef41Sopenharmony_ci "name": "register", 1011cb0ef41Sopenharmony_ci "meta": { 1021cb0ef41Sopenharmony_ci "added": [ 1031cb0ef41Sopenharmony_ci "v18.19.0" 1041cb0ef41Sopenharmony_ci ], 1051cb0ef41Sopenharmony_ci "changes": [ 1061cb0ef41Sopenharmony_ci { 1071cb0ef41Sopenharmony_ci "version": "v18.19.0", 1081cb0ef41Sopenharmony_ci "pr-url": "https://github.com/nodejs/node/pull/49655", 1091cb0ef41Sopenharmony_ci "description": "Add support for WHATWG URL instances." 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci ] 1121cb0ef41Sopenharmony_ci }, 1131cb0ef41Sopenharmony_ci "stability": 1, 1141cb0ef41Sopenharmony_ci "stabilityText": ".1 - Active development", 1151cb0ef41Sopenharmony_ci "signatures": [ 1161cb0ef41Sopenharmony_ci { 1171cb0ef41Sopenharmony_ci "params": [ 1181cb0ef41Sopenharmony_ci { 1191cb0ef41Sopenharmony_ci "textRaw": "`specifier` {string|URL} Customization hooks to be registered; this should be the same string that would be passed to `import()`, except that if it is relative, it is resolved relative to `parentURL`.", 1201cb0ef41Sopenharmony_ci "name": "specifier", 1211cb0ef41Sopenharmony_ci "type": "string|URL", 1221cb0ef41Sopenharmony_ci "desc": "Customization hooks to be registered; this should be the same string that would be passed to `import()`, except that if it is relative, it is resolved relative to `parentURL`." 1231cb0ef41Sopenharmony_ci }, 1241cb0ef41Sopenharmony_ci { 1251cb0ef41Sopenharmony_ci "textRaw": "`parentURL` {string|URL} If you want to resolve `specifier` relative to a base URL, such as `import.meta.url`, you can pass that URL here. **Default:** `'data:'`", 1261cb0ef41Sopenharmony_ci "name": "parentURL", 1271cb0ef41Sopenharmony_ci "type": "string|URL", 1281cb0ef41Sopenharmony_ci "default": "`'data:'`", 1291cb0ef41Sopenharmony_ci "desc": "If you want to resolve `specifier` relative to a base URL, such as `import.meta.url`, you can pass that URL here." 1301cb0ef41Sopenharmony_ci }, 1311cb0ef41Sopenharmony_ci { 1321cb0ef41Sopenharmony_ci "textRaw": "`options` {Object}", 1331cb0ef41Sopenharmony_ci "name": "options", 1341cb0ef41Sopenharmony_ci "type": "Object", 1351cb0ef41Sopenharmony_ci "options": [ 1361cb0ef41Sopenharmony_ci { 1371cb0ef41Sopenharmony_ci "textRaw": "`data` {any} Any arbitrary, cloneable JavaScript value to pass into the [`initialize`][] hook.", 1381cb0ef41Sopenharmony_ci "name": "data", 1391cb0ef41Sopenharmony_ci "type": "any", 1401cb0ef41Sopenharmony_ci "desc": "Any arbitrary, cloneable JavaScript value to pass into the [`initialize`][] hook." 1411cb0ef41Sopenharmony_ci }, 1421cb0ef41Sopenharmony_ci { 1431cb0ef41Sopenharmony_ci "textRaw": "`transferList` {Object\\[]} [transferrable objects][] to be passed into the `initialize` hook.", 1441cb0ef41Sopenharmony_ci "name": "transferList", 1451cb0ef41Sopenharmony_ci "type": "Object\\[]", 1461cb0ef41Sopenharmony_ci "desc": "[transferrable objects][] to be passed into the `initialize` hook." 1471cb0ef41Sopenharmony_ci } 1481cb0ef41Sopenharmony_ci ] 1491cb0ef41Sopenharmony_ci } 1501cb0ef41Sopenharmony_ci ] 1511cb0ef41Sopenharmony_ci } 1521cb0ef41Sopenharmony_ci ], 1531cb0ef41Sopenharmony_ci "desc": "<p>Register a module that exports <a href=\"#customization-hooks\">hooks</a> that customize Node.js module\nresolution and loading behavior. See <a href=\"#customization-hooks\">Customization hooks</a>.</p>" 1541cb0ef41Sopenharmony_ci }, 1551cb0ef41Sopenharmony_ci { 1561cb0ef41Sopenharmony_ci "textRaw": "`module.syncBuiltinESMExports()`", 1571cb0ef41Sopenharmony_ci "type": "method", 1581cb0ef41Sopenharmony_ci "name": "syncBuiltinESMExports", 1591cb0ef41Sopenharmony_ci "meta": { 1601cb0ef41Sopenharmony_ci "added": [ 1611cb0ef41Sopenharmony_ci "v12.12.0" 1621cb0ef41Sopenharmony_ci ], 1631cb0ef41Sopenharmony_ci "changes": [] 1641cb0ef41Sopenharmony_ci }, 1651cb0ef41Sopenharmony_ci "signatures": [ 1661cb0ef41Sopenharmony_ci { 1671cb0ef41Sopenharmony_ci "params": [] 1681cb0ef41Sopenharmony_ci } 1691cb0ef41Sopenharmony_ci ], 1701cb0ef41Sopenharmony_ci "desc": "<p>The <code>module.syncBuiltinESMExports()</code> method updates all the live bindings for\nbuiltin <a href=\"esm.html\">ES Modules</a> to match the properties of the <a href=\"modules.html\">CommonJS</a> exports. It\ndoes not add or remove exported names from the <a href=\"esm.html\">ES Modules</a>.</p>\n<pre><code class=\"language-js\">const fs = require('node:fs');\nconst assert = require('node:assert');\nconst { syncBuiltinESMExports } = require('node:module');\n\nfs.readFile = newAPI;\n\ndelete fs.readFileSync;\n\nfunction newAPI() {\n // ...\n}\n\nfs.newAPI = newAPI;\n\nsyncBuiltinESMExports();\n\nimport('node:fs').then((esmFS) => {\n // It syncs the existing readFile property with the new value\n assert.strictEqual(esmFS.readFile, newAPI);\n // readFileSync has been deleted from the required fs\n assert.strictEqual('readFileSync' in fs, false);\n // syncBuiltinESMExports() does not remove readFileSync from esmFS\n assert.strictEqual('readFileSync' in esmFS, true);\n // syncBuiltinESMExports() does not add names\n assert.strictEqual(esmFS.newAPI, undefined);\n});\n</code></pre>\n<p><i id=\"module_customization_hooks\"></i></p>" 1711cb0ef41Sopenharmony_ci } 1721cb0ef41Sopenharmony_ci ], 1731cb0ef41Sopenharmony_ci "type": "module", 1741cb0ef41Sopenharmony_ci "displayName": "The `Module` object" 1751cb0ef41Sopenharmony_ci }, 1761cb0ef41Sopenharmony_ci { 1771cb0ef41Sopenharmony_ci "textRaw": "Source map v3 support", 1781cb0ef41Sopenharmony_ci "name": "source_map_v3_support", 1791cb0ef41Sopenharmony_ci "meta": { 1801cb0ef41Sopenharmony_ci "added": [ 1811cb0ef41Sopenharmony_ci "v13.7.0", 1821cb0ef41Sopenharmony_ci "v12.17.0" 1831cb0ef41Sopenharmony_ci ], 1841cb0ef41Sopenharmony_ci "changes": [] 1851cb0ef41Sopenharmony_ci }, 1861cb0ef41Sopenharmony_ci "stability": 1, 1871cb0ef41Sopenharmony_ci "stabilityText": "Experimental", 1881cb0ef41Sopenharmony_ci "desc": "<p>Helpers for interacting with the source map cache. This cache is\npopulated when source map parsing is enabled and\n<a href=\"https://sourcemaps.info/spec.html#h.lmz475t4mvbx\">source map include directives</a> are found in a modules' footer.</p>\n<p>To enable source map parsing, Node.js must be run with the flag\n<a href=\"cli.html#--enable-source-maps\"><code>--enable-source-maps</code></a>, or with code coverage enabled by setting\n<a href=\"cli.html#node_v8_coveragedir\"><code>NODE_V8_COVERAGE=dir</code></a>.</p>\n<pre><code class=\"language-mjs\">// module.mjs\n// In an ECMAScript module\nimport { findSourceMap, SourceMap } from 'node:module';\n</code></pre>\n<pre><code class=\"language-cjs\">// module.cjs\n// In a CommonJS module\nconst { findSourceMap, SourceMap } = require('node:module');\n</code></pre>\n<!-- Anchors to make sure old links find a target -->\n<p><a id=\"module_module_findsourcemap_path_error\"></a></p>", 1891cb0ef41Sopenharmony_ci "methods": [ 1901cb0ef41Sopenharmony_ci { 1911cb0ef41Sopenharmony_ci "textRaw": "`module.findSourceMap(path)`", 1921cb0ef41Sopenharmony_ci "type": "method", 1931cb0ef41Sopenharmony_ci "name": "findSourceMap", 1941cb0ef41Sopenharmony_ci "meta": { 1951cb0ef41Sopenharmony_ci "added": [ 1961cb0ef41Sopenharmony_ci "v13.7.0", 1971cb0ef41Sopenharmony_ci "v12.17.0" 1981cb0ef41Sopenharmony_ci ], 1991cb0ef41Sopenharmony_ci "changes": [] 2001cb0ef41Sopenharmony_ci }, 2011cb0ef41Sopenharmony_ci "signatures": [ 2021cb0ef41Sopenharmony_ci { 2031cb0ef41Sopenharmony_ci "return": { 2041cb0ef41Sopenharmony_ci "textRaw": "Returns: {module.SourceMap|undefined} Returns `module.SourceMap` if a source map is found, `undefined` otherwise.", 2051cb0ef41Sopenharmony_ci "name": "return", 2061cb0ef41Sopenharmony_ci "type": "module.SourceMap|undefined", 2071cb0ef41Sopenharmony_ci "desc": "Returns `module.SourceMap` if a source map is found, `undefined` otherwise." 2081cb0ef41Sopenharmony_ci }, 2091cb0ef41Sopenharmony_ci "params": [ 2101cb0ef41Sopenharmony_ci { 2111cb0ef41Sopenharmony_ci "textRaw": "`path` {string}", 2121cb0ef41Sopenharmony_ci "name": "path", 2131cb0ef41Sopenharmony_ci "type": "string" 2141cb0ef41Sopenharmony_ci } 2151cb0ef41Sopenharmony_ci ] 2161cb0ef41Sopenharmony_ci } 2171cb0ef41Sopenharmony_ci ], 2181cb0ef41Sopenharmony_ci "desc": "<p><code>path</code> is the resolved path for the file for which a corresponding source map\nshould be fetched.</p>" 2191cb0ef41Sopenharmony_ci } 2201cb0ef41Sopenharmony_ci ], 2211cb0ef41Sopenharmony_ci "classes": [ 2221cb0ef41Sopenharmony_ci { 2231cb0ef41Sopenharmony_ci "textRaw": "Class: `module.SourceMap`", 2241cb0ef41Sopenharmony_ci "type": "class", 2251cb0ef41Sopenharmony_ci "name": "module.SourceMap", 2261cb0ef41Sopenharmony_ci "meta": { 2271cb0ef41Sopenharmony_ci "added": [ 2281cb0ef41Sopenharmony_ci "v13.7.0", 2291cb0ef41Sopenharmony_ci "v12.17.0" 2301cb0ef41Sopenharmony_ci ], 2311cb0ef41Sopenharmony_ci "changes": [] 2321cb0ef41Sopenharmony_ci }, 2331cb0ef41Sopenharmony_ci "properties": [ 2341cb0ef41Sopenharmony_ci { 2351cb0ef41Sopenharmony_ci "textRaw": "`payload` Returns: {Object}", 2361cb0ef41Sopenharmony_ci "type": "Object", 2371cb0ef41Sopenharmony_ci "name": "return", 2381cb0ef41Sopenharmony_ci "desc": "<p>Getter for the payload used to construct the <a href=\"#class-modulesourcemap\"><code>SourceMap</code></a> instance.</p>" 2391cb0ef41Sopenharmony_ci } 2401cb0ef41Sopenharmony_ci ], 2411cb0ef41Sopenharmony_ci "methods": [ 2421cb0ef41Sopenharmony_ci { 2431cb0ef41Sopenharmony_ci "textRaw": "`sourceMap.findEntry(lineOffset, columnOffset)`", 2441cb0ef41Sopenharmony_ci "type": "method", 2451cb0ef41Sopenharmony_ci "name": "findEntry", 2461cb0ef41Sopenharmony_ci "signatures": [ 2471cb0ef41Sopenharmony_ci { 2481cb0ef41Sopenharmony_ci "return": { 2491cb0ef41Sopenharmony_ci "textRaw": "Returns: {Object}", 2501cb0ef41Sopenharmony_ci "name": "return", 2511cb0ef41Sopenharmony_ci "type": "Object" 2521cb0ef41Sopenharmony_ci }, 2531cb0ef41Sopenharmony_ci "params": [ 2541cb0ef41Sopenharmony_ci { 2551cb0ef41Sopenharmony_ci "textRaw": "`lineOffset` {number} The zero-indexed line number offset in the generated source", 2561cb0ef41Sopenharmony_ci "name": "lineOffset", 2571cb0ef41Sopenharmony_ci "type": "number", 2581cb0ef41Sopenharmony_ci "desc": "The zero-indexed line number offset in the generated source" 2591cb0ef41Sopenharmony_ci }, 2601cb0ef41Sopenharmony_ci { 2611cb0ef41Sopenharmony_ci "textRaw": "`columnOffset` {number} The zero-indexed column number offset in the generated source", 2621cb0ef41Sopenharmony_ci "name": "columnOffset", 2631cb0ef41Sopenharmony_ci "type": "number", 2641cb0ef41Sopenharmony_ci "desc": "The zero-indexed column number offset in the generated source" 2651cb0ef41Sopenharmony_ci } 2661cb0ef41Sopenharmony_ci ] 2671cb0ef41Sopenharmony_ci } 2681cb0ef41Sopenharmony_ci ], 2691cb0ef41Sopenharmony_ci "desc": "<p>Given a line offset and column offset in the generated source\nfile, returns an object representing the SourceMap range in the\noriginal file if found, or an empty object if not.</p>\n<p>The object returned contains the following keys:</p>\n<ul>\n<li>generatedLine: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> The line offset of the start of the\nrange in the generated source</li>\n<li>generatedColumn: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> The column offset of start of the\nrange in the generated source</li>\n<li>originalSource: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> The file name of the original source,\nas reported in the SourceMap</li>\n<li>originalLine: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> The line offset of the start of the\nrange in the original source</li>\n<li>originalColumn: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> The column offset of start of the\nrange in the original source</li>\n<li>name: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a></li>\n</ul>\n<p>The returned value represents the raw range as it appears in the\nSourceMap, based on zero-indexed offsets, <em>not</em> 1-indexed line and\ncolumn numbers as they appear in Error messages and CallSite\nobjects.</p>\n<p>To get the corresponding 1-indexed line and column numbers from a\nlineNumber and columnNumber as they are reported by Error stacks\nand CallSite objects, use <code>sourceMap.findOrigin(lineNumber, columnNumber)</code></p>" 2701cb0ef41Sopenharmony_ci }, 2711cb0ef41Sopenharmony_ci { 2721cb0ef41Sopenharmony_ci "textRaw": "`sourceMap.findOrigin(lineNumber, columnNumber)`", 2731cb0ef41Sopenharmony_ci "type": "method", 2741cb0ef41Sopenharmony_ci "name": "findOrigin", 2751cb0ef41Sopenharmony_ci "signatures": [ 2761cb0ef41Sopenharmony_ci { 2771cb0ef41Sopenharmony_ci "return": { 2781cb0ef41Sopenharmony_ci "textRaw": "Returns: {Object}", 2791cb0ef41Sopenharmony_ci "name": "return", 2801cb0ef41Sopenharmony_ci "type": "Object" 2811cb0ef41Sopenharmony_ci }, 2821cb0ef41Sopenharmony_ci "params": [ 2831cb0ef41Sopenharmony_ci { 2841cb0ef41Sopenharmony_ci "textRaw": "`lineNumber` {number} The 1-indexed line number of the call site in the generated source", 2851cb0ef41Sopenharmony_ci "name": "lineNumber", 2861cb0ef41Sopenharmony_ci "type": "number", 2871cb0ef41Sopenharmony_ci "desc": "The 1-indexed line number of the call site in the generated source" 2881cb0ef41Sopenharmony_ci }, 2891cb0ef41Sopenharmony_ci { 2901cb0ef41Sopenharmony_ci "textRaw": "`columnOffset` {number} The 1-indexed column number of the call site in the generated source", 2911cb0ef41Sopenharmony_ci "name": "columnOffset", 2921cb0ef41Sopenharmony_ci "type": "number", 2931cb0ef41Sopenharmony_ci "desc": "The 1-indexed column number of the call site in the generated source" 2941cb0ef41Sopenharmony_ci } 2951cb0ef41Sopenharmony_ci ] 2961cb0ef41Sopenharmony_ci } 2971cb0ef41Sopenharmony_ci ], 2981cb0ef41Sopenharmony_ci "desc": "<p>Given a 1-indexed lineNumber and columnNumber from a call site in\nthe generated source, find the corresponding call site location\nin the original source.</p>\n<p>If the lineNumber and columnNumber provided are not found in any\nsource map, then an empty object is returned. Otherwise, the\nreturned object contains the following keys:</p>\n<ul>\n<li>name: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> | <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type\" class=\"type\"><undefined></a> The name of the range in the\nsource map, if one was provided</li>\n<li>fileName: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> The file name of the original source, as\nreported in the SourceMap</li>\n<li>lineNumber: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> The 1-indexed lineNumber of the\ncorresponding call site in the original source</li>\n<li>columnNumber: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a> The 1-indexed columnNumber of the\ncorresponding call site in the original source</li>\n</ul>" 2991cb0ef41Sopenharmony_ci } 3001cb0ef41Sopenharmony_ci ], 3011cb0ef41Sopenharmony_ci "signatures": [ 3021cb0ef41Sopenharmony_ci { 3031cb0ef41Sopenharmony_ci "params": [ 3041cb0ef41Sopenharmony_ci { 3051cb0ef41Sopenharmony_ci "textRaw": "`payload` {Object}", 3061cb0ef41Sopenharmony_ci "name": "payload", 3071cb0ef41Sopenharmony_ci "type": "Object" 3081cb0ef41Sopenharmony_ci } 3091cb0ef41Sopenharmony_ci ], 3101cb0ef41Sopenharmony_ci "desc": "<p>Creates a new <code>sourceMap</code> instance.</p>\n<p><code>payload</code> is an object with keys matching the <a href=\"https://sourcemaps.info/spec.html#h.mofvlxcwqzej\">Source map v3 format</a>:</p>\n<ul>\n<li><code>file</code>: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a></li>\n<li><code>version</code>: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type\" class=\"type\"><number></a></li>\n<li><code>sources</code>: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string[]></a></li>\n<li><code>sourcesContent</code>: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string[]></a></li>\n<li><code>names</code>: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string[]></a></li>\n<li><code>mappings</code>: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a></li>\n<li><code>sourceRoot</code>: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a></li>\n</ul>" 3111cb0ef41Sopenharmony_ci } 3121cb0ef41Sopenharmony_ci ] 3131cb0ef41Sopenharmony_ci } 3141cb0ef41Sopenharmony_ci ], 3151cb0ef41Sopenharmony_ci "type": "module", 3161cb0ef41Sopenharmony_ci "displayName": "Source map v3 support" 3171cb0ef41Sopenharmony_ci } 3181cb0ef41Sopenharmony_ci ], 3191cb0ef41Sopenharmony_ci "stability": 1, 3201cb0ef41Sopenharmony_ci "stabilityText": ".1 - Active development", 3211cb0ef41Sopenharmony_ci "miscs": [ 3221cb0ef41Sopenharmony_ci { 3231cb0ef41Sopenharmony_ci "textRaw": "Customization Hooks", 3241cb0ef41Sopenharmony_ci "name": "Customization Hooks", 3251cb0ef41Sopenharmony_ci "meta": { 3261cb0ef41Sopenharmony_ci "added": [ 3271cb0ef41Sopenharmony_ci "v8.8.0" 3281cb0ef41Sopenharmony_ci ], 3291cb0ef41Sopenharmony_ci "changes": [ 3301cb0ef41Sopenharmony_ci { 3311cb0ef41Sopenharmony_ci "version": "v18.19.0", 3321cb0ef41Sopenharmony_ci "pr-url": "https://github.com/nodejs/node/pull/48842", 3331cb0ef41Sopenharmony_ci "description": "Added `initialize` hook to replace `globalPreload`." 3341cb0ef41Sopenharmony_ci }, 3351cb0ef41Sopenharmony_ci { 3361cb0ef41Sopenharmony_ci "version": [ 3371cb0ef41Sopenharmony_ci "v18.6.0", 3381cb0ef41Sopenharmony_ci "v16.17.0" 3391cb0ef41Sopenharmony_ci ], 3401cb0ef41Sopenharmony_ci "pr-url": "https://github.com/nodejs/node/pull/42623", 3411cb0ef41Sopenharmony_ci "description": "Add support for chaining loaders." 3421cb0ef41Sopenharmony_ci }, 3431cb0ef41Sopenharmony_ci { 3441cb0ef41Sopenharmony_ci "version": "v16.12.0", 3451cb0ef41Sopenharmony_ci "pr-url": "https://github.com/nodejs/node/pull/37468", 3461cb0ef41Sopenharmony_ci "description": "Removed `getFormat`, `getSource`, `transformSource`, and `globalPreload`; added `load` hook and `getGlobalPreload` hook." 3471cb0ef41Sopenharmony_ci } 3481cb0ef41Sopenharmony_ci ] 3491cb0ef41Sopenharmony_ci }, 3501cb0ef41Sopenharmony_ci "stability": 1, 3511cb0ef41Sopenharmony_ci "stabilityText": ".1 - Active development", 3521cb0ef41Sopenharmony_ci "type": "misc", 3531cb0ef41Sopenharmony_ci "desc": "<p><i id=\"enabling_module_customization_hooks\"></i></p>", 3541cb0ef41Sopenharmony_ci "miscs": [ 3551cb0ef41Sopenharmony_ci { 3561cb0ef41Sopenharmony_ci "textRaw": "Enabling", 3571cb0ef41Sopenharmony_ci "name": "enabling", 3581cb0ef41Sopenharmony_ci "desc": "<p>Module resolution and loading can be customized by registering a file which\nexports a set of hooks. This can be done using the <a href=\"#moduleregisterspecifier-parenturl-options\"><code>register</code></a> method\nfrom <code>node:module</code>, which you can run before your application code by\nusing the <code>--import</code> flag:</p>\n<pre><code class=\"language-bash\">node --import ./register-hooks.js ./my-app.js\n</code></pre>\n<pre><code class=\"language-mjs\">// register-hooks.js\nimport { register } from 'node:module';\n\nregister('./hooks.mjs', import.meta.url);\n</code></pre>\n<pre><code class=\"language-cjs\">// register-hooks.js\nconst { register } = require('node:module');\nconst { pathToFileURL } = require('node:url');\n\nregister('./hooks.mjs', pathToFileURL(__filename));\n</code></pre>\n<p>The file passed to <code>--import</code> can also be an export from a dependency:</p>\n<pre><code class=\"language-bash\">node --import some-package/register ./my-app.js\n</code></pre>\n<p>Where <code>some-package</code> has an <a href=\"packages.html#exports\"><code>\"exports\"</code></a> field defining the <code>/register</code>\nexport to map to a file that calls <code>register()</code>, like the following <code>register-hooks.js</code>\nexample.</p>\n<p>Using <code>--import</code> ensures that the hooks are registered before any application\nfiles are imported, including the entry point of the application. Alternatively,\n<code>register</code> can be called from the entry point, but dynamic <code>import()</code> must be\nused for any code that should be run after the hooks are registered:</p>\n<pre><code class=\"language-mjs\">import { register } from 'node:module';\n\nregister('http-to-https', import.meta.url);\n\n// Because this is a dynamic `import()`, the `http-to-https` hooks will run\n// to handle `./my-app.js` and any other files it imports or requires.\nawait import('./my-app.js');\n</code></pre>\n<pre><code class=\"language-cjs\">const { register } = require('node:module');\nconst { pathToFileURL } = require('node:url');\n\nregister('http-to-https', pathToFileURL(__filename));\n\n// Because this is a dynamic `import()`, the `http-to-https` hooks will run\n// to handle `./my-app.js` and any other files it imports or requires.\nimport('./my-app.js');\n</code></pre>\n<p>In this example, we are registering the <code>http-to-https</code> hooks, but they will\nonly be available for subsequently imported modules—in this case, <code>my-app.js</code>\nand anything it references via <code>import</code> (and optionally <code>require</code>). If the\n<code>import('./my-app.js')</code> had instead been a static <code>import './my-app.js'</code>, the\napp would have <em>already</em> been loaded <strong>before</strong> the <code>http-to-https</code> hooks were\nregistered. This due to the ES modules specification, where static imports are\nevaluated from the leaves of the tree first, then back to the trunk. There can\nbe static imports <em>within</em> <code>my-app.js</code>, which will not be evaluated until\n<code>my-app.js</code> is dynamically imported.</p>\n<p><code>my-app.js</code> can also be CommonJS. Customization hooks will run for any\nmodules that it references via <code>import</code> (and optionally <code>require</code>).</p>\n<p>Finally, if all you want to do is register hooks before your app runs and you\ndon't want to create a separate file for that purpose, you can pass a <code>data:</code>\nURL to <code>--import</code>:</p>\n<pre><code class=\"language-bash\">node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"http-to-https\", pathToFileURL(\"./\"));' ./my-app.js\n</code></pre>", 3591cb0ef41Sopenharmony_ci "type": "misc", 3601cb0ef41Sopenharmony_ci "displayName": "Enabling" 3611cb0ef41Sopenharmony_ci }, 3621cb0ef41Sopenharmony_ci { 3631cb0ef41Sopenharmony_ci "textRaw": "Chaining", 3641cb0ef41Sopenharmony_ci "name": "chaining", 3651cb0ef41Sopenharmony_ci "desc": "<p>It's possible to call <code>register</code> more than once:</p>\n<pre><code class=\"language-mjs\">// entrypoint.mjs\nimport { register } from 'node:module';\n\nregister('./first.mjs', import.meta.url);\nregister('./second.mjs', import.meta.url);\nawait import('./my-app.mjs');\n</code></pre>\n<pre><code class=\"language-cjs\">// entrypoint.cjs\nconst { register } = require('node:module');\nconst { pathToFileURL } = require('node:url');\n\nconst parentURL = pathToFileURL(__filename);\nregister('./first.mjs', parentURL);\nregister('./second.mjs', parentURL);\nimport('./my-app.mjs');\n</code></pre>\n<p>In this example, the registered hooks will form chains. If both <code>first.mjs</code> and\n<code>second.mjs</code> define a <code>resolve</code> hook, both will be called, in the order they\nwere registered. The same applies to all the other hooks.</p>\n<p>The registered hooks also affect <code>register</code> itself. In this example,\n<code>second.mjs</code> will be resolved and loaded per the hooks registered by\n<code>first.mjs</code>. This allows for things like writing hooks in non-JavaScript\nlanguages, so long as an earlier registered loader is one that transpiles into\nJavaScript.</p>\n<p>The <code>register</code> method cannot be called from within the module that defines the\nhooks.</p>", 3661cb0ef41Sopenharmony_ci "type": "misc", 3671cb0ef41Sopenharmony_ci "displayName": "Chaining" 3681cb0ef41Sopenharmony_ci }, 3691cb0ef41Sopenharmony_ci { 3701cb0ef41Sopenharmony_ci "textRaw": "Communication with module customization hooks", 3711cb0ef41Sopenharmony_ci "name": "communication_with_module_customization_hooks", 3721cb0ef41Sopenharmony_ci "desc": "<p>Module customization hooks run on a dedicated thread, separate from the main\nthread that runs application code. This means mutating global variables won't\naffect the other thread(s), and message channels must be used to communicate\nbetween the threads.</p>\n<p>The <code>register</code> method can be used to pass data to an <a href=\"#initialize\"><code>initialize</code></a> hook. The\ndata passed to the hook may include transferrable objects like ports.</p>\n<pre><code class=\"language-mjs\">import { register } from 'node:module';\nimport { MessageChannel } from 'node:worker_threads';\n\n// This example demonstrates how a message channel can be used to\n// communicate with the hooks, by sending `port2` to the hooks.\nconst { port1, port2 } = new MessageChannel();\n\nport1.on('message', (msg) => {\n console.log(msg);\n});\n\nregister('./my-hooks.mjs', {\n parentURL: import.meta.url,\n data: { number: 1, port: port2 },\n transferList: [port2],\n});\n</code></pre>\n<pre><code class=\"language-cjs\">const { register } = require('node:module');\nconst { pathToFileURL } = require('node:url');\nconst { MessageChannel } = require('node:worker_threads');\n\n// This example showcases how a message channel can be used to\n// communicate with the hooks, by sending `port2` to the hooks.\nconst { port1, port2 } = new MessageChannel();\n\nport1.on('message', (msg) => {\n console.log(msg);\n});\n\nregister('./my-hooks.mjs', {\n parentURL: pathToFileURL(__filename),\n data: { number: 1, port: port2 },\n transferList: [port2],\n});\n</code></pre>", 3731cb0ef41Sopenharmony_ci "type": "misc", 3741cb0ef41Sopenharmony_ci "displayName": "Communication with module customization hooks" 3751cb0ef41Sopenharmony_ci }, 3761cb0ef41Sopenharmony_ci { 3771cb0ef41Sopenharmony_ci "textRaw": "Hooks", 3781cb0ef41Sopenharmony_ci "name": "hooks", 3791cb0ef41Sopenharmony_ci "desc": "<p>The <a href=\"#moduleregisterspecifier-parenturl-options\"><code>register</code></a> method can be used to register a module that exports a set of\nhooks. The hooks are functions that are called by Node.js to customize the\nmodule resolution and loading process. The exported functions must have specific\nnames and signatures, and they must be exported as named exports.</p>\n<pre><code class=\"language-mjs\">export async function initialize({ number, port }) {\n // Receives data from `register`.\n}\n\nexport async function resolve(specifier, context, nextResolve) {\n // Take an `import` or `require` specifier and resolve it to a URL.\n}\n\nexport async function load(url, context, nextLoad) {\n // Take a resolved URL and return the source code to be evaluated.\n}\n</code></pre>\n<p>Hooks are part of a chain, even if that chain consists of only one custom\n(user-provided) hook and the default hook, which is always present. Hook\nfunctions nest: each one must always return a plain object, and chaining happens\nas a result of each function calling <code>next<hookName>()</code>, which is a reference to\nthe subsequent loader's hook.</p>\n<p>A hook that returns a value lacking a required property triggers an exception. A\nhook that returns without calling <code>next<hookName>()</code> <em>and</em> without returning\n<code>shortCircuit: true</code> also triggers an exception. These errors are to help\nprevent unintentional breaks in the chain. Return <code>shortCircuit: true</code> from a\nhook to signal that the chain is intentionally ending at your hook.</p>\n<p>Hooks are run in a separate thread, isolated from the main thread where\napplication code runs. That means it is a different <a href=\"https://tc39.es/ecma262/#realm\">realm</a>. The hooks thread\nmay be terminated by the main thread at any time, so do not depend on\nasynchronous operations (like <code>console.log</code>) to complete.</p>", 3801cb0ef41Sopenharmony_ci "methods": [ 3811cb0ef41Sopenharmony_ci { 3821cb0ef41Sopenharmony_ci "textRaw": "`initialize()`", 3831cb0ef41Sopenharmony_ci "type": "method", 3841cb0ef41Sopenharmony_ci "name": "initialize", 3851cb0ef41Sopenharmony_ci "meta": { 3861cb0ef41Sopenharmony_ci "added": [ 3871cb0ef41Sopenharmony_ci "v18.19.0" 3881cb0ef41Sopenharmony_ci ], 3891cb0ef41Sopenharmony_ci "changes": [] 3901cb0ef41Sopenharmony_ci }, 3911cb0ef41Sopenharmony_ci "stability": 1, 3921cb0ef41Sopenharmony_ci "stabilityText": ".1 - Active development", 3931cb0ef41Sopenharmony_ci "signatures": [ 3941cb0ef41Sopenharmony_ci { 3951cb0ef41Sopenharmony_ci "params": [ 3961cb0ef41Sopenharmony_ci { 3971cb0ef41Sopenharmony_ci "textRaw": "`data` {any} The data from `register(loader, import.meta.url, { data })`.", 3981cb0ef41Sopenharmony_ci "name": "data", 3991cb0ef41Sopenharmony_ci "type": "any", 4001cb0ef41Sopenharmony_ci "desc": "The data from `register(loader, import.meta.url, { data })`." 4011cb0ef41Sopenharmony_ci } 4021cb0ef41Sopenharmony_ci ] 4031cb0ef41Sopenharmony_ci } 4041cb0ef41Sopenharmony_ci ], 4051cb0ef41Sopenharmony_ci "desc": "<p>The <code>initialize</code> hook provides a way to define a custom function that runs in\nthe hooks thread when the hooks module is initialized. Initialization happens\nwhen the hooks module is registered via <a href=\"#moduleregisterspecifier-parenturl-options\"><code>register</code></a>.</p>\n<p>This hook can receive data from a <a href=\"#moduleregisterspecifier-parenturl-options\"><code>register</code></a> invocation, including\nports and other transferrable objects. The return value of <code>initialize</code> can be a\n<a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise\" class=\"type\"><Promise></a>, in which case it will be awaited before the main application thread\nexecution resumes.</p>\n<p>Module customization code:</p>\n<pre><code class=\"language-mjs\">// path-to-my-hooks.js\n\nexport async function initialize({ number, port }) {\n port.postMessage(`increment: ${number + 1}`);\n}\n</code></pre>\n<p>Caller code:</p>\n<pre><code class=\"language-mjs\">import assert from 'node:assert';\nimport { register } from 'node:module';\nimport { MessageChannel } from 'node:worker_threads';\n\n// This example showcases how a message channel can be used to communicate\n// between the main (application) thread and the hooks running on the hooks\n// thread, by sending `port2` to the `initialize` hook.\nconst { port1, port2 } = new MessageChannel();\n\nport1.on('message', (msg) => {\n assert.strictEqual(msg, 'increment: 2');\n});\n\nregister('./path-to-my-hooks.js', {\n parentURL: import.meta.url,\n data: { number: 1, port: port2 },\n transferList: [port2],\n});\n</code></pre>\n<pre><code class=\"language-cjs\">const assert = require('node:assert');\nconst { register } = require('node:module');\nconst { pathToFileURL } = require('node:url');\nconst { MessageChannel } = require('node:worker_threads');\n\n// This example showcases how a message channel can be used to communicate\n// between the main (application) thread and the hooks running on the hooks\n// thread, by sending `port2` to the `initialize` hook.\nconst { port1, port2 } = new MessageChannel();\n\nport1.on('message', (msg) => {\n assert.strictEqual(msg, 'increment: 2');\n});\n\nregister('./path-to-my-hooks.js', {\n parentURL: pathToFileURL(__filename),\n data: { number: 1, port: port2 },\n transferList: [port2],\n});\n</code></pre>" 4061cb0ef41Sopenharmony_ci }, 4071cb0ef41Sopenharmony_ci { 4081cb0ef41Sopenharmony_ci "textRaw": "`resolve(specifier, context, nextResolve)`", 4091cb0ef41Sopenharmony_ci "type": "method", 4101cb0ef41Sopenharmony_ci "name": "resolve", 4111cb0ef41Sopenharmony_ci "meta": { 4121cb0ef41Sopenharmony_ci "changes": [ 4131cb0ef41Sopenharmony_ci { 4141cb0ef41Sopenharmony_ci "version": "v18.19.0", 4151cb0ef41Sopenharmony_ci "pr-url": "https://github.com/nodejs/node/pull/50140", 4161cb0ef41Sopenharmony_ci "description": "The property `context.importAssertions` is replaced with `context.importAttributes`. Using the old name is still supported and will emit an experimental warning." 4171cb0ef41Sopenharmony_ci }, 4181cb0ef41Sopenharmony_ci { 4191cb0ef41Sopenharmony_ci "version": [ 4201cb0ef41Sopenharmony_ci "v18.6.0", 4211cb0ef41Sopenharmony_ci "v16.17.0" 4221cb0ef41Sopenharmony_ci ], 4231cb0ef41Sopenharmony_ci "pr-url": "https://github.com/nodejs/node/pull/42623", 4241cb0ef41Sopenharmony_ci "description": "Add support for chaining resolve hooks. Each hook must either call `nextResolve()` or include a `shortCircuit` property set to `true` in its return." 4251cb0ef41Sopenharmony_ci }, 4261cb0ef41Sopenharmony_ci { 4271cb0ef41Sopenharmony_ci "version": [ 4281cb0ef41Sopenharmony_ci "v17.1.0", 4291cb0ef41Sopenharmony_ci "v16.14.0" 4301cb0ef41Sopenharmony_ci ], 4311cb0ef41Sopenharmony_ci "pr-url": "https://github.com/nodejs/node/pull/40250", 4321cb0ef41Sopenharmony_ci "description": "Add support for import assertions." 4331cb0ef41Sopenharmony_ci } 4341cb0ef41Sopenharmony_ci ] 4351cb0ef41Sopenharmony_ci }, 4361cb0ef41Sopenharmony_ci "stability": 1, 4371cb0ef41Sopenharmony_ci "stabilityText": ".2 - Release candidate", 4381cb0ef41Sopenharmony_ci "signatures": [ 4391cb0ef41Sopenharmony_ci { 4401cb0ef41Sopenharmony_ci "return": { 4411cb0ef41Sopenharmony_ci "textRaw": "Returns: {Object|Promise}", 4421cb0ef41Sopenharmony_ci "name": "return", 4431cb0ef41Sopenharmony_ci "type": "Object|Promise", 4441cb0ef41Sopenharmony_ci "options": [ 4451cb0ef41Sopenharmony_ci { 4461cb0ef41Sopenharmony_ci "textRaw": "`format` {string|null|undefined} A hint to the load hook (it might be ignored) `'builtin' | 'commonjs' | 'json' | 'module' | 'wasm'`", 4471cb0ef41Sopenharmony_ci "name": "format", 4481cb0ef41Sopenharmony_ci "type": "string|null|undefined", 4491cb0ef41Sopenharmony_ci "desc": "A hint to the load hook (it might be ignored) `'builtin' | 'commonjs' | 'json' | 'module' | 'wasm'`" 4501cb0ef41Sopenharmony_ci }, 4511cb0ef41Sopenharmony_ci { 4521cb0ef41Sopenharmony_ci "textRaw": "`importAttributes` {Object|undefined} The import attributes to use when caching the module (optional; if excluded the input will be used)", 4531cb0ef41Sopenharmony_ci "name": "importAttributes", 4541cb0ef41Sopenharmony_ci "type": "Object|undefined", 4551cb0ef41Sopenharmony_ci "desc": "The import attributes to use when caching the module (optional; if excluded the input will be used)" 4561cb0ef41Sopenharmony_ci }, 4571cb0ef41Sopenharmony_ci { 4581cb0ef41Sopenharmony_ci "textRaw": "`shortCircuit` {undefined|boolean} A signal that this hook intends to terminate the chain of `resolve` hooks. **Default:** `false`", 4591cb0ef41Sopenharmony_ci "name": "shortCircuit", 4601cb0ef41Sopenharmony_ci "type": "undefined|boolean", 4611cb0ef41Sopenharmony_ci "default": "`false`", 4621cb0ef41Sopenharmony_ci "desc": "A signal that this hook intends to terminate the chain of `resolve` hooks." 4631cb0ef41Sopenharmony_ci }, 4641cb0ef41Sopenharmony_ci { 4651cb0ef41Sopenharmony_ci "textRaw": "`url` {string} The absolute URL to which this input resolves", 4661cb0ef41Sopenharmony_ci "name": "url", 4671cb0ef41Sopenharmony_ci "type": "string", 4681cb0ef41Sopenharmony_ci "desc": "The absolute URL to which this input resolves" 4691cb0ef41Sopenharmony_ci } 4701cb0ef41Sopenharmony_ci ] 4711cb0ef41Sopenharmony_ci }, 4721cb0ef41Sopenharmony_ci "params": [ 4731cb0ef41Sopenharmony_ci { 4741cb0ef41Sopenharmony_ci "textRaw": "`specifier` {string}", 4751cb0ef41Sopenharmony_ci "name": "specifier", 4761cb0ef41Sopenharmony_ci "type": "string" 4771cb0ef41Sopenharmony_ci }, 4781cb0ef41Sopenharmony_ci { 4791cb0ef41Sopenharmony_ci "textRaw": "`context` {Object}", 4801cb0ef41Sopenharmony_ci "name": "context", 4811cb0ef41Sopenharmony_ci "type": "Object", 4821cb0ef41Sopenharmony_ci "options": [ 4831cb0ef41Sopenharmony_ci { 4841cb0ef41Sopenharmony_ci "textRaw": "`conditions` {string\\[]} Export conditions of the relevant `package.json`", 4851cb0ef41Sopenharmony_ci "name": "conditions", 4861cb0ef41Sopenharmony_ci "type": "string\\[]", 4871cb0ef41Sopenharmony_ci "desc": "Export conditions of the relevant `package.json`" 4881cb0ef41Sopenharmony_ci }, 4891cb0ef41Sopenharmony_ci { 4901cb0ef41Sopenharmony_ci "textRaw": "`importAttributes` {Object} An object whose key-value pairs represent the attributes for the module to import", 4911cb0ef41Sopenharmony_ci "name": "importAttributes", 4921cb0ef41Sopenharmony_ci "type": "Object", 4931cb0ef41Sopenharmony_ci "desc": "An object whose key-value pairs represent the attributes for the module to import" 4941cb0ef41Sopenharmony_ci }, 4951cb0ef41Sopenharmony_ci { 4961cb0ef41Sopenharmony_ci "textRaw": "`parentURL` {string|undefined} The module importing this one, or undefined if this is the Node.js entry point", 4971cb0ef41Sopenharmony_ci "name": "parentURL", 4981cb0ef41Sopenharmony_ci "type": "string|undefined", 4991cb0ef41Sopenharmony_ci "desc": "The module importing this one, or undefined if this is the Node.js entry point" 5001cb0ef41Sopenharmony_ci } 5011cb0ef41Sopenharmony_ci ] 5021cb0ef41Sopenharmony_ci }, 5031cb0ef41Sopenharmony_ci { 5041cb0ef41Sopenharmony_ci "textRaw": "`nextResolve` {Function} The subsequent `resolve` hook in the chain, or the Node.js default `resolve` hook after the last user-supplied `resolve` hook", 5051cb0ef41Sopenharmony_ci "name": "nextResolve", 5061cb0ef41Sopenharmony_ci "type": "Function", 5071cb0ef41Sopenharmony_ci "desc": "The subsequent `resolve` hook in the chain, or the Node.js default `resolve` hook after the last user-supplied `resolve` hook", 5081cb0ef41Sopenharmony_ci "options": [ 5091cb0ef41Sopenharmony_ci { 5101cb0ef41Sopenharmony_ci "textRaw": "`specifier` {string}", 5111cb0ef41Sopenharmony_ci "name": "specifier", 5121cb0ef41Sopenharmony_ci "type": "string" 5131cb0ef41Sopenharmony_ci }, 5141cb0ef41Sopenharmony_ci { 5151cb0ef41Sopenharmony_ci "textRaw": "`context` {Object}", 5161cb0ef41Sopenharmony_ci "name": "context", 5171cb0ef41Sopenharmony_ci "type": "Object" 5181cb0ef41Sopenharmony_ci } 5191cb0ef41Sopenharmony_ci ] 5201cb0ef41Sopenharmony_ci } 5211cb0ef41Sopenharmony_ci ] 5221cb0ef41Sopenharmony_ci } 5231cb0ef41Sopenharmony_ci ], 5241cb0ef41Sopenharmony_ci "desc": "<blockquote>\n<p><strong>Warning</strong> Despite support for returning promises and async functions, calls\nto <code>resolve</code> may block the main thread which can impact performance.</p>\n</blockquote>\n<p>The <code>resolve</code> hook chain is responsible for telling Node.js where to find and\nhow to cache a given <code>import</code> statement or expression, or <code>require</code> call. It can\noptionally return a format (such as <code>'module'</code>) as a hint to the <code>load</code> hook. If\na format is specified, the <code>load</code> hook is ultimately responsible for providing\nthe final <code>format</code> value (and it is free to ignore the hint provided by\n<code>resolve</code>); if <code>resolve</code> provides a <code>format</code>, a custom <code>load</code> hook is required\neven if only to pass the value to the Node.js default <code>load</code> hook.</p>\n<p>Import type attributes are part of the cache key for saving loaded modules into\nthe internal module cache. The <code>resolve</code> hook is responsible for returning an\n<code>importAttributes</code> object if the module should be cached with different\nattributes than were present in the source code.</p>\n<p>The <code>conditions</code> property in <code>context</code> is an array of conditions for\n<a href=\"packages.html#conditional-exports\">package exports conditions</a> that apply to this resolution\nrequest. They can be used for looking up conditional mappings elsewhere or to\nmodify the list when calling the default resolution logic.</p>\n<p>The current <a href=\"packages.html#conditional-exports\">package exports conditions</a> are always in\nthe <code>context.conditions</code> array passed into the hook. To guarantee <em>default\nNode.js module specifier resolution behavior</em> when calling <code>defaultResolve</code>, the\n<code>context.conditions</code> array passed to it <em>must</em> include <em>all</em> elements of the\n<code>context.conditions</code> array originally passed into the <code>resolve</code> hook.</p>\n<pre><code class=\"language-mjs\">export async function resolve(specifier, context, nextResolve) {\n const { parentURL = null } = context;\n\n if (Math.random() > 0.5) { // Some condition.\n // For some or all specifiers, do some custom logic for resolving.\n // Always return an object of the form {url: <string>}.\n return {\n shortCircuit: true,\n url: parentURL ?\n new URL(specifier, parentURL).href :\n new URL(specifier).href,\n };\n }\n\n if (Math.random() < 0.5) { // Another condition.\n // When calling `defaultResolve`, the arguments can be modified. In this\n // case it's adding another value for matching conditional exports.\n return nextResolve(specifier, {\n ...context,\n conditions: [...context.conditions, 'another-condition'],\n });\n }\n\n // Defer to the next hook in the chain, which would be the\n // Node.js default resolve if this is the last user-specified loader.\n return nextResolve(specifier);\n}\n</code></pre>" 5251cb0ef41Sopenharmony_ci }, 5261cb0ef41Sopenharmony_ci { 5271cb0ef41Sopenharmony_ci "textRaw": "`load(url, context, nextLoad)`", 5281cb0ef41Sopenharmony_ci "type": "method", 5291cb0ef41Sopenharmony_ci "name": "load", 5301cb0ef41Sopenharmony_ci "meta": { 5311cb0ef41Sopenharmony_ci "changes": [ 5321cb0ef41Sopenharmony_ci { 5331cb0ef41Sopenharmony_ci "version": [ 5341cb0ef41Sopenharmony_ci "v18.6.0", 5351cb0ef41Sopenharmony_ci "v16.17.0" 5361cb0ef41Sopenharmony_ci ], 5371cb0ef41Sopenharmony_ci "pr-url": "https://github.com/nodejs/node/pull/42623", 5381cb0ef41Sopenharmony_ci "description": "Add support for chaining load hooks. Each hook must either call `nextLoad()` or include a `shortCircuit` property set to `true` in its return." 5391cb0ef41Sopenharmony_ci } 5401cb0ef41Sopenharmony_ci ] 5411cb0ef41Sopenharmony_ci }, 5421cb0ef41Sopenharmony_ci "stability": 1, 5431cb0ef41Sopenharmony_ci "stabilityText": ".2 - Release candidate", 5441cb0ef41Sopenharmony_ci "signatures": [ 5451cb0ef41Sopenharmony_ci { 5461cb0ef41Sopenharmony_ci "return": { 5471cb0ef41Sopenharmony_ci "textRaw": "Returns: {Object}", 5481cb0ef41Sopenharmony_ci "name": "return", 5491cb0ef41Sopenharmony_ci "type": "Object", 5501cb0ef41Sopenharmony_ci "options": [ 5511cb0ef41Sopenharmony_ci { 5521cb0ef41Sopenharmony_ci "textRaw": "`format` {string}", 5531cb0ef41Sopenharmony_ci "name": "format", 5541cb0ef41Sopenharmony_ci "type": "string" 5551cb0ef41Sopenharmony_ci }, 5561cb0ef41Sopenharmony_ci { 5571cb0ef41Sopenharmony_ci "textRaw": "`shortCircuit` {undefined|boolean} A signal that this hook intends to terminate the chain of `resolve` hooks. **Default:** `false`", 5581cb0ef41Sopenharmony_ci "name": "shortCircuit", 5591cb0ef41Sopenharmony_ci "type": "undefined|boolean", 5601cb0ef41Sopenharmony_ci "default": "`false`", 5611cb0ef41Sopenharmony_ci "desc": "A signal that this hook intends to terminate the chain of `resolve` hooks." 5621cb0ef41Sopenharmony_ci }, 5631cb0ef41Sopenharmony_ci { 5641cb0ef41Sopenharmony_ci "textRaw": "`source` {string|ArrayBuffer|TypedArray} The source for Node.js to evaluate", 5651cb0ef41Sopenharmony_ci "name": "source", 5661cb0ef41Sopenharmony_ci "type": "string|ArrayBuffer|TypedArray", 5671cb0ef41Sopenharmony_ci "desc": "The source for Node.js to evaluate" 5681cb0ef41Sopenharmony_ci } 5691cb0ef41Sopenharmony_ci ] 5701cb0ef41Sopenharmony_ci }, 5711cb0ef41Sopenharmony_ci "params": [ 5721cb0ef41Sopenharmony_ci { 5731cb0ef41Sopenharmony_ci "textRaw": "`url` {string} The URL returned by the `resolve` chain", 5741cb0ef41Sopenharmony_ci "name": "url", 5751cb0ef41Sopenharmony_ci "type": "string", 5761cb0ef41Sopenharmony_ci "desc": "The URL returned by the `resolve` chain" 5771cb0ef41Sopenharmony_ci }, 5781cb0ef41Sopenharmony_ci { 5791cb0ef41Sopenharmony_ci "textRaw": "`context` {Object}", 5801cb0ef41Sopenharmony_ci "name": "context", 5811cb0ef41Sopenharmony_ci "type": "Object", 5821cb0ef41Sopenharmony_ci "options": [ 5831cb0ef41Sopenharmony_ci { 5841cb0ef41Sopenharmony_ci "textRaw": "`conditions` {string\\[]} Export conditions of the relevant `package.json`", 5851cb0ef41Sopenharmony_ci "name": "conditions", 5861cb0ef41Sopenharmony_ci "type": "string\\[]", 5871cb0ef41Sopenharmony_ci "desc": "Export conditions of the relevant `package.json`" 5881cb0ef41Sopenharmony_ci }, 5891cb0ef41Sopenharmony_ci { 5901cb0ef41Sopenharmony_ci "textRaw": "`format` {string|null|undefined} The format optionally supplied by the `resolve` hook chain", 5911cb0ef41Sopenharmony_ci "name": "format", 5921cb0ef41Sopenharmony_ci "type": "string|null|undefined", 5931cb0ef41Sopenharmony_ci "desc": "The format optionally supplied by the `resolve` hook chain" 5941cb0ef41Sopenharmony_ci }, 5951cb0ef41Sopenharmony_ci { 5961cb0ef41Sopenharmony_ci "textRaw": "`importAttributes` {Object}", 5971cb0ef41Sopenharmony_ci "name": "importAttributes", 5981cb0ef41Sopenharmony_ci "type": "Object" 5991cb0ef41Sopenharmony_ci } 6001cb0ef41Sopenharmony_ci ] 6011cb0ef41Sopenharmony_ci }, 6021cb0ef41Sopenharmony_ci { 6031cb0ef41Sopenharmony_ci "textRaw": "`nextLoad` {Function} The subsequent `load` hook in the chain, or the Node.js default `load` hook after the last user-supplied `load` hook", 6041cb0ef41Sopenharmony_ci "name": "nextLoad", 6051cb0ef41Sopenharmony_ci "type": "Function", 6061cb0ef41Sopenharmony_ci "desc": "The subsequent `load` hook in the chain, or the Node.js default `load` hook after the last user-supplied `load` hook", 6071cb0ef41Sopenharmony_ci "options": [ 6081cb0ef41Sopenharmony_ci { 6091cb0ef41Sopenharmony_ci "textRaw": "`specifier` {string}", 6101cb0ef41Sopenharmony_ci "name": "specifier", 6111cb0ef41Sopenharmony_ci "type": "string" 6121cb0ef41Sopenharmony_ci }, 6131cb0ef41Sopenharmony_ci { 6141cb0ef41Sopenharmony_ci "textRaw": "`context` {Object}", 6151cb0ef41Sopenharmony_ci "name": "context", 6161cb0ef41Sopenharmony_ci "type": "Object" 6171cb0ef41Sopenharmony_ci } 6181cb0ef41Sopenharmony_ci ] 6191cb0ef41Sopenharmony_ci } 6201cb0ef41Sopenharmony_ci ] 6211cb0ef41Sopenharmony_ci } 6221cb0ef41Sopenharmony_ci ], 6231cb0ef41Sopenharmony_ci "desc": "<p>The <code>load</code> hook provides a way to define a custom method of determining how a\nURL should be interpreted, retrieved, and parsed. It is also in charge of\nvalidating the import assertion.</p>\n<p>The final value of <code>format</code> must be one of the following:</p>\n<table>\n<thead>\n<tr>\n<th><code>format</code></th>\n<th>Description</th>\n<th>Acceptable types for <code>source</code> returned by <code>load</code></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>'builtin'</code></td>\n<td>Load a Node.js builtin module</td>\n<td>Not applicable</td>\n</tr>\n<tr>\n<td><code>'commonjs'</code></td>\n<td>Load a Node.js CommonJS module</td>\n<td>Not applicable</td>\n</tr>\n<tr>\n<td><code>'json'</code></td>\n<td>Load a JSON file</td>\n<td>{ <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String\"><code>string</code></a>, <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer\"><code>ArrayBuffer</code></a>, <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a> }</td>\n</tr>\n<tr>\n<td><code>'module'</code></td>\n<td>Load an ES module</td>\n<td>{ <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String\"><code>string</code></a>, <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer\"><code>ArrayBuffer</code></a>, <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a> }</td>\n</tr>\n<tr>\n<td><code>'wasm'</code></td>\n<td>Load a WebAssembly module</td>\n<td>{ <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer\"><code>ArrayBuffer</code></a>, <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a> }</td>\n</tr>\n</tbody>\n</table>\n<p>The value of <code>source</code> is ignored for type <code>'builtin'</code> because currently it is\nnot possible to replace the value of a Node.js builtin (core) module. The value\nof <code>source</code> is ignored for type <code>'commonjs'</code> because the CommonJS module loader\ndoes not provide a mechanism for the ES module loader to override the\n<a href=\"esm.html#commonjs-namespaces\">CommonJS module return value</a>. This limitation\nmight be overcome in the future.</p>\n<blockquote>\n<p><strong>Warning</strong>: The ESM <code>load</code> hook and namespaced exports from CommonJS modules\nare incompatible. Attempting to use them together will result in an empty\nobject from the import. This may be addressed in the future.</p>\n</blockquote>\n<blockquote>\n<p>These types all correspond to classes defined in ECMAScript.</p>\n</blockquote>\n<ul>\n<li>The specific <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer\"><code>ArrayBuffer</code></a> object is a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer\"><code>SharedArrayBuffer</code></a>.</li>\n<li>The specific <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\"><code>TypedArray</code></a> object is a <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array\"><code>Uint8Array</code></a>.</li>\n</ul>\n<p>If the source value of a text-based format (i.e., <code>'json'</code>, <code>'module'</code>)\nis not a string, it is converted to a string using <a href=\"util.html#class-utiltextdecoder\"><code>util.TextDecoder</code></a>.</p>\n<p>The <code>load</code> hook provides a way to define a custom method for retrieving the\nsource code of a resolved URL. This would allow a loader to potentially avoid\nreading files from disk. It could also be used to map an unrecognized format to\na supported one, for example <code>yaml</code> to <code>module</code>.</p>\n<pre><code class=\"language-mjs\">export async function load(url, context, nextLoad) {\n const { format } = context;\n\n if (Math.random() > 0.5) { // Some condition\n /*\n For some or all URLs, do some custom logic for retrieving the source.\n Always return an object of the form {\n format: <string>,\n source: <string|buffer>,\n }.\n */\n return {\n format,\n shortCircuit: true,\n source: '...',\n };\n }\n\n // Defer to the next hook in the chain.\n return nextLoad(url);\n}\n</code></pre>\n<p>In a more advanced scenario, this can also be used to transform an unsupported\nsource to a supported one (see <a href=\"#examples\">Examples</a> below).</p>" 6241cb0ef41Sopenharmony_ci }, 6251cb0ef41Sopenharmony_ci { 6261cb0ef41Sopenharmony_ci "textRaw": "`globalPreload()`", 6271cb0ef41Sopenharmony_ci "type": "method", 6281cb0ef41Sopenharmony_ci "name": "globalPreload", 6291cb0ef41Sopenharmony_ci "meta": { 6301cb0ef41Sopenharmony_ci "changes": [ 6311cb0ef41Sopenharmony_ci { 6321cb0ef41Sopenharmony_ci "version": [ 6331cb0ef41Sopenharmony_ci "v18.6.0", 6341cb0ef41Sopenharmony_ci "v16.17.0" 6351cb0ef41Sopenharmony_ci ], 6361cb0ef41Sopenharmony_ci "pr-url": "https://github.com/nodejs/node/pull/42623", 6371cb0ef41Sopenharmony_ci "description": "Add support for chaining globalPreload hooks." 6381cb0ef41Sopenharmony_ci } 6391cb0ef41Sopenharmony_ci ] 6401cb0ef41Sopenharmony_ci }, 6411cb0ef41Sopenharmony_ci "stability": 1, 6421cb0ef41Sopenharmony_ci "stabilityText": ".0 - Early development", 6431cb0ef41Sopenharmony_ci "signatures": [ 6441cb0ef41Sopenharmony_ci { 6451cb0ef41Sopenharmony_ci "params": [] 6461cb0ef41Sopenharmony_ci } 6471cb0ef41Sopenharmony_ci ], 6481cb0ef41Sopenharmony_ci "desc": "<blockquote>\n<p><strong>Warning:</strong> This hook will be removed in a future version. Use\n<a href=\"#initialize\"><code>initialize</code></a> instead. When a hooks module has an <code>initialize</code> export,\n<code>globalPreload</code> will be ignored.</p>\n</blockquote>\n<ul>\n<li><code>context</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\"><Object></a> Information to assist the preload code\n<ul>\n<li><code>port</code> <a href=\"worker_threads.html#class-messageport\" class=\"type\"><MessagePort></a></li>\n</ul>\n</li>\n<li>Returns: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\"><string></a> Code to run before application startup</li>\n</ul>\n<p>Sometimes it might be necessary to run some code inside of the same global\nscope that the application runs in. This hook allows the return of a string\nthat is run as a sloppy-mode script on startup.</p>\n<p>Similar to how CommonJS wrappers work, the code runs in an implicit function\nscope. The only argument is a <code>require</code>-like function that can be used to load\nbuiltins like \"fs\": <code>getBuiltin(request: string)</code>.</p>\n<p>If the code needs more advanced <code>require</code> features, it has to construct\nits own <code>require</code> using <code>module.createRequire()</code>.</p>\n<pre><code class=\"language-mjs\">export function globalPreload(context) {\n return `\\\nglobalThis.someInjectedProperty = 42;\nconsole.log('I just set some globals!');\n\nconst { createRequire } = getBuiltin('module');\nconst { cwd } = getBuiltin('process');\n\nconst require = createRequire(cwd() + '/<preload>');\n// [...]\n`;\n}\n</code></pre>\n<p>Another argument is provided to the preload code: <code>port</code>. This is available as a\nparameter to the hook and inside of the source text returned by the hook. This\nfunctionality has been moved to the <code>initialize</code> hook.</p>\n<p>Care must be taken in order to properly call <a href=\"worker_threads.html#portref\"><code>port.ref()</code></a> and\n<a href=\"worker_threads.html#portunref\"><code>port.unref()</code></a> to prevent a process from being in a state where it won't\nclose normally.</p>\n<pre><code class=\"language-mjs\">/**\n * This example has the application context send a message to the hook\n * and sends the message back to the application context\n */\nexport function globalPreload({ port }) {\n port.on('message', (msg) => {\n port.postMessage(msg);\n });\n return `\\\n port.postMessage('console.log(\"I went to the hook and back\");');\n port.on('message', (msg) => {\n eval(msg);\n });\n `;\n}\n</code></pre>\n<h3>Examples</h3>\n<p>The various module customization hooks can be used together to accomplish\nwide-ranging customizations of the Node.js code loading and evaluation\nbehaviors.</p>" 6491cb0ef41Sopenharmony_ci } 6501cb0ef41Sopenharmony_ci ], 6511cb0ef41Sopenharmony_ci "modules": [ 6521cb0ef41Sopenharmony_ci { 6531cb0ef41Sopenharmony_ci "textRaw": "Import from HTTPS", 6541cb0ef41Sopenharmony_ci "name": "import_from_https", 6551cb0ef41Sopenharmony_ci "desc": "<p>In current Node.js, specifiers starting with <code>https://</code> are experimental (see\n<a href=\"esm.html#https-and-http-imports\">HTTPS and HTTP imports</a>).</p>\n<p>The hook below registers hooks to enable rudimentary support for such\nspecifiers. While this may seem like a significant improvement to Node.js core\nfunctionality, there are substantial downsides to actually using these hooks:\nperformance is much slower than loading files from disk, there is no caching,\nand there is no security.</p>\n<pre><code class=\"language-mjs\">// https-hooks.mjs\nimport { get } from 'node:https';\n\nexport function load(url, context, nextLoad) {\n // For JavaScript to be loaded over the network, we need to fetch and\n // return it.\n if (url.startsWith('https://')) {\n return new Promise((resolve, reject) => {\n get(url, (res) => {\n let data = '';\n res.setEncoding('utf8');\n res.on('data', (chunk) => data += chunk);\n res.on('end', () => resolve({\n // This example assumes all network-provided JavaScript is ES module\n // code.\n format: 'module',\n shortCircuit: true,\n source: data,\n }));\n }).on('error', (err) => reject(err));\n });\n }\n\n // Let Node.js handle all other URLs.\n return nextLoad(url);\n}\n</code></pre>\n<pre><code class=\"language-mjs\">// main.mjs\nimport { VERSION } from 'https://coffeescript.org/browser-compiler-modern/coffeescript.js';\n\nconsole.log(VERSION);\n</code></pre>\n<p>With the preceding hooks module, running\n<code>node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(pathToFileURL(\"./https-hooks.mjs\"));' ./main.mjs</code>\nprints the current version of CoffeeScript per the module at the URL in\n<code>main.mjs</code>.</p>", 6561cb0ef41Sopenharmony_ci "type": "module", 6571cb0ef41Sopenharmony_ci "displayName": "Import from HTTPS" 6581cb0ef41Sopenharmony_ci }, 6591cb0ef41Sopenharmony_ci { 6601cb0ef41Sopenharmony_ci "textRaw": "Transpilation", 6611cb0ef41Sopenharmony_ci "name": "transpilation", 6621cb0ef41Sopenharmony_ci "desc": "<p>Sources that are in formats Node.js doesn't understand can be converted into\nJavaScript using the <a href=\"#loadurl-context-nextload\"><code>load</code> hook</a>.</p>\n<p>This is less performant than transpiling source files before running Node.js;\ntranspiler hooks should only be used for development and testing purposes.</p>\n<pre><code class=\"language-mjs\">// coffeescript-hooks.mjs\nimport { readFile } from 'node:fs/promises';\nimport { dirname, extname, resolve as resolvePath } from 'node:path';\nimport { cwd } from 'node:process';\nimport { fileURLToPath, pathToFileURL } from 'node:url';\nimport coffeescript from 'coffeescript';\n\nconst extensionsRegex = /\\.(coffee|litcoffee|coffee\\.md)$/;\n\nexport async function load(url, context, nextLoad) {\n if (extensionsRegex.test(url)) {\n // CoffeeScript files can be either CommonJS or ES modules, so we want any\n // CoffeeScript file to be treated by Node.js the same as a .js file at the\n // same location. To determine how Node.js would interpret an arbitrary .js\n // file, search up the file system for the nearest parent package.json file\n // and read its \"type\" field.\n const format = await getPackageType(url);\n\n const { source: rawSource } = await nextLoad(url, { ...context, format });\n // This hook converts CoffeeScript source code into JavaScript source code\n // for all imported CoffeeScript files.\n const transformedSource = coffeescript.compile(rawSource.toString(), url);\n\n return {\n format,\n shortCircuit: true,\n source: transformedSource,\n };\n }\n\n // Let Node.js handle all other URLs.\n return nextLoad(url);\n}\n\nasync function getPackageType(url) {\n // `url` is only a file path during the first iteration when passed the\n // resolved url from the load() hook\n // an actual file path from load() will contain a file extension as it's\n // required by the spec\n // this simple truthy check for whether `url` contains a file extension will\n // work for most projects but does not cover some edge-cases (such as\n // extensionless files or a url ending in a trailing space)\n const isFilePath = !!extname(url);\n // If it is a file path, get the directory it's in\n const dir = isFilePath ?\n dirname(fileURLToPath(url)) :\n url;\n // Compose a file path to a package.json in the same directory,\n // which may or may not exist\n const packagePath = resolvePath(dir, 'package.json');\n // Try to read the possibly nonexistent package.json\n const type = await readFile(packagePath, { encoding: 'utf8' })\n .then((filestring) => JSON.parse(filestring).type)\n .catch((err) => {\n if (err?.code !== 'ENOENT') console.error(err);\n });\n // Ff package.json existed and contained a `type` field with a value, voila\n if (type) return type;\n // Otherwise, (if not at the root) continue checking the next directory up\n // If at the root, stop and return false\n return dir.length > 1 && getPackageType(resolvePath(dir, '..'));\n}\n</code></pre>\n<pre><code class=\"language-coffee\"># main.coffee\nimport { scream } from './scream.coffee'\nconsole.log scream 'hello, world'\n\nimport { version } from 'node:process'\nconsole.log \"Brought to you by Node.js version #{version}\"\n</code></pre>\n<pre><code class=\"language-coffee\"># scream.coffee\nexport scream = (str) -> str.toUpperCase()\n</code></pre>\n<p>With the preceding hooks module, running\n<code>node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(pathToFileURL(\"./coffeescript-hooks.mjs\"));' ./main.coffee</code>\ncauses <code>main.coffee</code> to be turned into JavaScript after its source code is\nloaded from disk but before Node.js executes it; and so on for any <code>.coffee</code>,\n<code>.litcoffee</code> or <code>.coffee.md</code> files referenced via <code>import</code> statements of any\nloaded file.</p>", 6631cb0ef41Sopenharmony_ci "type": "module", 6641cb0ef41Sopenharmony_ci "displayName": "Transpilation" 6651cb0ef41Sopenharmony_ci }, 6661cb0ef41Sopenharmony_ci { 6671cb0ef41Sopenharmony_ci "textRaw": "Import maps", 6681cb0ef41Sopenharmony_ci "name": "import_maps", 6691cb0ef41Sopenharmony_ci "desc": "<p>The previous two examples defined <code>load</code> hooks. This is an example of a\n<code>resolve</code> hook. This hooks module reads an <code>import-map.json</code> file that defines\nwhich specifiers to override to other URLs (this is a very simplistic\nimplementation of a small subset of the \"import maps\" specification).</p>\n<pre><code class=\"language-mjs\">// import-map-hooks.js\nimport fs from 'node:fs/promises';\n\nconst { imports } = JSON.parse(await fs.readFile('import-map.json'));\n\nexport async function resolve(specifier, context, nextResolve) {\n if (Object.hasOwn(imports, specifier)) {\n return nextResolve(imports[specifier], context);\n }\n\n return nextResolve(specifier, context);\n}\n</code></pre>\n<p>With these files:</p>\n<pre><code class=\"language-mjs\">// main.js\nimport 'a-module';\n</code></pre>\n<pre><code class=\"language-json\">// import-map.json\n{\n \"imports\": {\n \"a-module\": \"./some-module.js\"\n }\n}\n</code></pre>\n<pre><code class=\"language-mjs\">// some-module.js\nconsole.log('some module!');\n</code></pre>\n<p>Running <code>node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(pathToFileURL(\"./import-map-hooks.js\"));' main.js</code>\nshould print <code>some module!</code>.</p>", 6701cb0ef41Sopenharmony_ci "type": "module", 6711cb0ef41Sopenharmony_ci "displayName": "Import maps" 6721cb0ef41Sopenharmony_ci } 6731cb0ef41Sopenharmony_ci ], 6741cb0ef41Sopenharmony_ci "type": "misc", 6751cb0ef41Sopenharmony_ci "displayName": "Hooks" 6761cb0ef41Sopenharmony_ci } 6771cb0ef41Sopenharmony_ci ] 6781cb0ef41Sopenharmony_ci } 6791cb0ef41Sopenharmony_ci ], 6801cb0ef41Sopenharmony_ci "type": "module", 6811cb0ef41Sopenharmony_ci "displayName": "Modules: `node:module` API" 6821cb0ef41Sopenharmony_ci } 6831cb0ef41Sopenharmony_ci ] 6841cb0ef41Sopenharmony_ci}