Node.js Reference ยท path, os and url
Node.js os.EOL and os.constants
Reference for os.EOL and os.constants in Node.js: platform line endings, signal numbers, errno codes and priority values, with practical examples.
The os module exports a few constants. os.EOL is the line separator of the current platform: "\n" on Linux and macOS, "\r\n" on Windows. os.constants is an object with numeric constants grouped into signals, errno, priority and dlopen, plus a few extras such as UV_UDP_REUSEADDR.
These values matter when you write text meant for the local system, translate a signal name into its number or interpret the numeric error from a low-level call. Most Node.js code uses string names ('SIGTERM', 'ENOENT'), and os.constants is the bridge to the numbers.
import os from "node:os"; console.log(JSON.stringify(os.EOL)); console.log(os.constants.signals.SIGINT);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Constants
| Name | Description |
|---|---|
os.EOL |
Platform line ending: "\n" (POSIX) or "\r\n" (Windows). |
os.constants.signals |
Signal names mapped to numbers, for example SIGINT (2), SIGTERM (15), SIGKILL (9), SIGHUP (1). |
os.constants.errno |
Error names such as ENOENT, EACCES, EEXIST mapped to numbers. |
os.constants.priority |
Process priority levels used by os.setPriority() and os.getPriority(). |
os.constants.dlopen |
Flags for loading native add-ons with process.dlopen() (not present on Windows). |
Notes on values: signal numbers for SIGINT, SIGTERM, SIGKILL and SIGHUP are the same on every platform that defines them. Others, such as SIGUSR1 and SIGCHLD, have different numbers on Linux and macOS, which is the reason to read them from the object instead of hard-coding them. The errno numbers also differ between platforms, and on Windows they hold libuv's own values.
Usage notes
os.EOLonly for output meant for the local machine. Files exchanged between systems should use a fixed"\n", and readers should split on/\r?\n/.- Do not search for
os.EOLwhen parsing. A file written on Windows can be read on Linux, and"\r\n"will not be found by splitting on"\n"alone. - Signal lists are platform specific. Windows defines only a handful (
SIGINT,SIGTERM,SIGKILL,SIGHUP,SIGBREAK, ...). - Use string names with Node.js APIs.
process.kill(pid, "SIGTERM")anderr.code === "ENOENT"are clearer than numbers; useos.constantsfor exit-code arithmetic (128 + signal) and for logs. - Priorities:
os.setPriority(pid, os.constants.priority.PRIORITY_BELOW_NORMAL)lowers the priority of a process; raising it usually needs elevated rights. - Constants are read-only data. Treat
os.constantsas frozen.