• Node.js Video Tutorials

Node.js - 內建模組



Node.js 中的模組是獨立且可重用的程式碼集合,可以匯入到任何 Node.js 應用程式中。Node.js 執行時軟體包含 V8 JavaScript 引擎,以及許多核心模組,這些模組執行重要的伺服器端任務,例如管理事件迴圈、執行檔案 I/O 和特定於作業系統的功能等。

示例

以下程式碼片段返回所有內建模組的列表:

 const builtinModules = require('repl')._builtinLibs;
 console.log(builtinModules);

輸出

[
  'assert',             'assert/strict',       'async_hooks',
  'buffer',             'child_process',       'cluster',
  'console',            'constants',           'crypto',
  'dgram',              'diagnostics_channel', 'dns',
  'dns/promises',       'domain',              'events',
  'fs',                 'fs/promises',         'http',
  'http2',              'https',               'inspector',
  'inspector/promises', 'module',              'net',
  'os',                 'path',                'path/posix',
  'path/win32',         'perf_hooks',          'process',
  'punycode',           'querystring',         'readline',
  'readline/promises',  'repl',                'stream',
  'stream/consumers',   'stream/promises',     'stream/web',
  'string_decoder',     'sys',                 'timers',
  'timers/promises',    'tls',                 'trace_events',
  'tty',                'url',                 'util',
  'util/types',         'v8',                  'vm',
  'wasi',               'worker_threads',      'zlib'
]

Node.js 擁有許多核心模組,這些模組提供了構建應用程式的基本功能。以下是其中一些最重要的核心模組列表:

序號 核心模組及描述
1

assert

提供一組斷言函式,用於驗證不變數。

2

buffer

Buffer 物件表示固定長度的位元組序列。

3

http

提供用於建立 HTTP 伺服器和發出 HTTP 請求的介面。

4

fs

提供用於處理檔案和目錄的函式。

5

path

提供用於處理檔案路徑的函式。

6

url

提供用於解析和構建 URL 的函式。

7

util

提供用於處理資料和字串的實用程式函式。

8

crypto

提供用於加密和安全雜湊的函式。

9

process

提供有關當前 Node.js 程序的資訊,並允許您與作業系統互動。

10

net

提供低階網路功能。

11

stream

提供用於處理資料流的基本框架。

12

events

提供一個事件發射器類,用於自定義事件處理。

13

console

提供用於寫入控制檯的函式。

14

readline

提供用於從流中逐行讀取的函式。

15

query String

提供用於解析和格式化 URL 查詢字串的實用程式。

16

v8

提供特定於 v8 版本的函式。

17

os

提供相關的函式和屬性。

廣告