ข้ามไปยังเนื้อหา

Core APIs

Node.js มาพร้อม globals และโมดูลที่ติดมาในตัว (built-in) ที่สามารถใช้งานได้โดยไม่ต้องติดตั้งอะไรเพิ่ม สิ่งเหล่านี้คือ primitives ที่โปรแกรม Node.js ทุกตัวสร้างขึ้นมาบน

globals และโมดูลหลักที่โมดูลนี้จะครอบคลุม:

  • process — เข้าถึงข้อมูลของ process ที่กำลังทำงาน: environment variables, command-line arguments, working directory, platform และการควบคุม lifecycle
  • Buffer — ชิ้นส่วนของข้อมูลไบนารีดิบที่มีขนาดคงที่ มีมาก่อน typed arrays และยังคงเป็นวิธีมาตรฐานในการจัดการ bytes ใน Node streams และ networking
  • node:fs / node:fs/promises — อ่าน เขียน และนำทางในระบบไฟล์ promises API ทำให้ใช้ async/await ได้โดยตรง
  • EventEmitter — แกนหลักของสถาปัตยกรรม event-driven ของ Node Streams, HTTP server และ library ของ third-party หลายตัวล้วน extend EventEmitter
บทเรียนหัวข้อ
process & envprocess.argv, process.env, exit codes และการตรวจจับ platform
Bufferข้อมูลไบนารี, encoding และการจัดการระดับ byte
File Systemcallbacks ของ fs, sync APIs และ interface fs/promises
EventEmittercustom events, listeners และ lifecycle ของ once / removeListener

snippet ด้านล่างอ่าน globals สองตัวที่มีให้ใช้ใน Node.js process ทุกตัวโดยไม่ต้อง require หรือ import:

Node.js

Needs the Node.js runtime — open in StackBlitz to run.

อันไหนคือ Node.js global แท้จริงที่ใช้ได้โดยไม่ต้อง import?
prefix สมัยใหม่ที่แนะนำสำหรับการ import โมดูล built-in ของ Node.js คืออะไร?