Node.js v11.14.0 文档


os(操作系统)#

中英对照提交修改

稳定性: 2 - 稳定

os 模块提供了操作系统相关的实用方法。 使用方法如下:

const os = require('os');

os.EOL#

中英对照提交修改

一个字符串常量,定义操作系统相关的行末标志:

  • \n 在 POSIX 系统上
  • \r\n 在 Windows系统上

os.arch()#

中英对照提交修改

os.arch()方法返回一个字符串, 表明 Node.js 二进制编译所用的 操作系统CPU架构.

现在可能的值有: 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', 'x64'

等价于 process.arch.

os.constants#

中英对照提交修改

返回一个包含错误码,处理信号等通用的操作系统特定常量的对象. 现在, 这些特定的常量的定义被描述在OS Constants

os.cpus()#

中英对照提交修改

os.cpus() 方法返回一个对象数组, 包含每个逻辑 CPU 内核的信息.

下面的属性包含在每个对象中:

  • model <string>
  • speed <number> (兆赫兹为单位)
  • times <Object>

    • user <number> CPU花费在用户模式下的毫秒时间数.
    • nice <number> CPU花费在良好模式下的毫秒时间数.
    • sys <number> CPU花费在系统模式下的毫秒时间数.
    • idle <number> CPU花费在空闲模式下的毫秒时间数.
    • irq <number> CPU花费在中断请求模式下的毫秒时间数.

For example:

[
  {
    model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times: {
      user: 252020,
      nice: 0,
      sys: 30340,
      idle: 1070356870,
      irq: 0
    }
  },
  {
    model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times: {
      user: 306960,
      nice: 0,
      sys: 26980,
      idle: 1071569080,
      irq: 0
    }
  },
  {
    model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times: {
      user: 248450,
      nice: 0,
      sys: 21750,
      idle: 1070919370,
      irq: 0
    }
  },
  {
    model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times: {
      user: 256880,
      nice: 0,
      sys: 19430,
      idle: 1070905480,
      irq: 20
    }
  },
  {
    model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times: {
      user: 511580,
      nice: 20,
      sys: 40900,
      idle: 1070842510,
      irq: 0
    }
  },
  {
    model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times: {
      user: 291660,
      nice: 0,
      sys: 34360,
      idle: 1070888000,
      irq: 10
    }
  },
  {
    model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times: {
      user: 308260,
      nice: 0,
      sys: 55410,
      idle: 1071129970,
      irq: 880
    }
  },
  {
    model: 'Intel(R) Core(TM) i7 CPU         860  @ 2.80GHz',
    speed: 2926,
    times: {
      user: 266450,
      nice: 1480,
      sys: 34920,
      idle: 1072572010,
      irq: 30
    }
  }
]

注意: 因为nice的值是UNIX相关的, 在Windows系统上, 所有处理器的 nice 值总是0.

os.endianness()#

中英对照提交修改

os.endianness()方法返回一个字符串,表明Node.js二进制编译环境的字节顺序.

可能的值:

  • 'BE' 大端模式
  • 'LE' 小端模式

os.freemem()#

中英对照提交修改

os.freemem() 方法以整数的形式回空闲系统内存 的字节数.

os.getPriority([pid])#

暂无中英对照提交修改

  • pid <integer> The process ID to retrieve scheduling priority for. Default 0.
  • Returns: <integer>

The os.getPriority() method returns the scheduling priority for the process specified by pid. If pid is not provided, or is 0, the priority of the current process is returned.

os.homedir()#

中英对照提交修改

os.homedir() 方法以字符串的形式返回当前用户的home目录.

os.hostname()#

中英对照提交修改

os.hostname()方法以字符串的形式返回操作系统的主机名.

os.loadavg()#

中英对照提交修改

os.loadavg()方法返回一个数组,包含1, 5, 15分钟平均负载.

平均负载是系统活动的测量,由操作系统计算得出,表达为一个分数. 一般来说,平均负载应该理想地比系统的逻辑CPU的数目要少. 平均负载是UNIX相关的概念,在Windows平台上没有对应的概念. 在Windows上,其返回值总是[0, 0, 0].

os.networkInterfaces()#

查看v10.x中文文档

The os.networkInterfaces() method returns an object containing only network interfaces that have been assigned a network address.

Each key on the returned object identifies a network interface. The associated value is an array of objects that each describe an assigned network address.

The properties available on the assigned network address object include:

  • address <string> The assigned IPv4 or IPv6 address
  • netmask <string> The IPv4 or IPv6 network mask
  • family <string> Either IPv4 or IPv6
  • mac <string> The MAC address of the network interface
  • internal <boolean> true if the network interface is a loopback or similar interface that is not remotely accessible; otherwise false
  • scopeid <number> The numeric IPv6 scope ID (only specified when family is IPv6)
  • cidr <string> The assigned IPv4 or IPv6 address with the routing prefix in CIDR notation. If the netmask is invalid, this property is set to null.
{
  lo: [
    {
      address: '127.0.0.1',
      netmask: '255.0.0.0',
      family: 'IPv4',
      mac: '00:00:00:00:00:00',
      internal: true,
      cidr: '127.0.0.1/8'
    },
    {
      address: '::1',
      netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
      family: 'IPv6',
      mac: '00:00:00:00:00:00',
      scopeid: 0,
      internal: true,
      cidr: '::1/128'
    }
  ],
  eth0: [
    {
      address: '192.168.1.108',
      netmask: '255.255.255.0',
      family: 'IPv4',
      mac: '01:02:03:0a:0b:0c',
      internal: false,
      cidr: '192.168.1.108/24'
    },
    {
      address: 'fe80::a00:27ff:fe4e:66a1',
      netmask: 'ffff:ffff:ffff:ffff::',
      family: 'IPv6',
      mac: '01:02:03:0a:0b:0c',
      scopeid: 1,
      internal: false,
      cidr: 'fe80::a00:27ff:fe4e:66a1/64'
    }
  ]
}

os.platform()#

中英对照提交修改

os.platform() 方法返回一个字符串, 指定Node.js编译时的操作系统平台

当前可能的值有:

  • 'aix'
  • 'darwin'
  • 'freebsd'
  • 'linux'
  • 'openbsd'
  • 'sunos'
  • 'win32'

等价于 process.platform.

注意: 如果Node.js 在Android操作系统上构建, 'android'值 可能被返回. 然而, Android支持Node.js在当前被认为是实验期.

os.release()#

中英对照提交修改

os.release()方法返回一个字符串, 指定操作系统的发行版.

注意: 在POSIX系统上, 操作系统发行版是通过 调用 uname(3) 得到的. 在 Windows系统上, 用GetVersionExW() . 请查看 https://en.wikipedia.org/wiki/Uname#Examples 获取更多信息.

os.setPriority([pid, ]priority)#

暂无中英对照提交修改

  • pid <integer> The process ID to set scheduling priority for. Default 0.
  • priority <integer> The scheduling priority to assign to the process.

The os.setPriority() method attempts to set the scheduling priority for the process specified by pid. If pid is not provided, or is 0, the priority of the current process is used.

The priority input must be an integer between -20 (high priority) and 19 (low priority). Due to differences between Unix priority levels and Windows priority classes, priority is mapped to one of six priority constants in os.constants.priority. When retrieving a process priority level, this range mapping may cause the return value to be slightly different on Windows. To avoid confusion, it is recommended to set priority to one of the priority constants.

On Windows setting priority to PRIORITY_HIGHEST requires elevated user, otherwise the set priority will be silently reduced to PRIORITY_HIGH.

os.tmpdir()#

中英对照提交修改

os.tmpdir()方法返回一个字符串, 表明操作系统的 默认临时文件目录.

os.totalmem()#

中英对照提交修改

os.totalmem()方法以整数的形式返回所有系统内存的字节数.

os.type()#

中英对照提交修改

os.type()方法返回一个字符串,表明操作系统的名字, 由 uname(3) 返回.举个例子, 'Linux' 在 Linux系统上, 'Darwin' 在 macOS 系统上,'Windows_NT' 在 Windows系统上.

请查看https://en.wikipedia.org/wiki/Uname#Examples 获取其他关于在不同 操作系统上执行 uname(3),得到输出的信息.

os.uptime()#

中英对照提交修改

os.uptime() 方法在几秒内返回操作系统的上线时间.

注意:在Windows平台上,这个方法返回的秒值包含分数,请用Math.floor()获取整数值

os.userInfo([options])#

查看v10.x中文文档

  • options <Object>

    • encoding <string> Character encoding used to interpret resulting strings. If encoding is set to 'buffer', the username, shell, and homedir values will be Buffer instances. Default: 'utf8'.
  • Returns: <Object>

The os.userInfo() method returns information about the currently effective user — on POSIX platforms, this is typically a subset of the password file. The returned object includes the username, uid, gid, shell, and homedir. On Windows, the uid and gid fields are -1, and shell is null.

The value of homedir returned by os.userInfo() is provided by the operating system. This differs from the result of os.homedir(), which queries several environment variables for the home directory before falling back to the operating system response.

Throws a [SystemError][] if a user has no username or homedir.

OS 常量#

中英对照提交修改

下面的常量被os.constants 所输出.

注意: 并不是所有的常量在每一个操作系统上都是可用的.

信号常量#

中英对照提交修改

下面的信号常量由 os.constants.signals 给出:

   
常量 描述
SIGHUP 发送来表明当一个控制终端关闭或者是父进程退出.
SIGINT发送来表明当一个用户期望中断一个进程时. (`(Ctrl+C)`).
SIGQUIT 发送来表明当一个用户希望终止一个进程并且执行核心转储.
SIGILL 发送给一个进程来通知它已经试图执行一个非法的,畸形的,未知的或特权的指令.
SIGTRAP 发送给一个进程当异常已经发生了.
SIGABRT 发送给一个进程来请求终止
SIGIOT SIGABRT的同义词
SIGBUS 发送给一个进程来通知它已经造成了总线错误.
SIGFPE 发送给一个进程来通知它已经执行了一个非法的算术操作.
SIGKILL 发送给一个进程来立即终止它.
SIGUSR1 SIGUSR2 发送给一个进程来确定它的用户定义情况.
SIGSEGV 发送给一个进程来通知段错误.
SIGPIPE 发送给一个进程当它试图写入一个非连接的管道时.
SIGALRM 发送给一个进程当系统时钟消逝时.
SIGTERM 发送给一个进程来请求终止.
SIGCHLD 发送给一个进程当一个子进程终止时.
SIGSTKFLT 发送给一个进程来表明一个协处理器的栈错误.
SIGCONT 发送来通知操作系统继续一个暂停的进程.
SIGSTOP 发送来通知操作系统暂停一个进程.
SIGTSTP 发送给一个进程来请求它停止.
SIGBREAK 发送来表明当一个用户希望终止一个进程.
SIGTTIN 发送给一个进程当它在后台读取TTY时.
SIGTTOU 发送给一个进程当它在后台写入TTY时.
SIGURG 发送给一个进程当socket由紧急的数据需要读取时.
SIGXCPU 发送给一个进程当它超过他在CPU使用上的限制时.
SIGXFSZ 发送给一个进程当它文件成长的比最大允许的值还大时.
SIGVTALRM 发送给一个进程当一个虚拟时钟消逝时.
SIGPROF 发送给一个进程当一个系统时钟消逝时.
SIGWINCH 发送给一个进程当控制终端改变它的大小.
SIGIO 发送给一个进程当I/O可用时.
SIGPOLL SIGIO同义词
SIGLOST 发送给一个进程当文件锁丢失时.
SIGPWR 发送给一个进程来通知功率错误.
SIGINFO SIGPWR同义词
SIGSYS 发送给一个进程来通知有错误的参数.
SIGUNUSED SIGSYS的同义词

错误常量#

中英对照提交修改

下面的错误常量由 os.constants.errno 给出:

POSIX 错误常量#

查看v10.x中文文档

Constant Description
E2BIG Indicates that the list of arguments is longer than expected.
EACCES Indicates that the operation did not have sufficient permissions.
EADDRINUSE Indicates that the network address is already in use.
EADDRNOTAVAIL Indicates that the network address is currently unavailable for use.
EAFNOSUPPORT Indicates that the network address family is not supported.
EAGAIN Indicates that there is currently no data available and to try the operation again later.
EALREADY Indicates that the socket already has a pending connection in progress.
EBADF Indicates that a file descriptor is not valid.
EBADMSG Indicates an invalid data message.
EBUSY Indicates that a device or resource is busy.
ECANCELED Indicates that an operation was canceled.
ECHILD Indicates that there are no child processes.
ECONNABORTED Indicates that the network connection has been aborted.
ECONNREFUSED Indicates that the network connection has been refused.
ECONNRESET Indicates that the network connection has been reset.
EDEADLK Indicates that a resource deadlock has been avoided.
EDESTADDRREQ Indicates that a destination address is required.
EDOM Indicates that an argument is out of the domain of the function.
EDQUOT Indicates that the disk quota has been exceeded.
EEXIST Indicates that the file already exists.
EFAULT Indicates an invalid pointer address.
EFBIG Indicates that the file is too large.
EHOSTUNREACH Indicates that the host is unreachable.
EIDRM Indicates that the identifier has been removed.
EILSEQ Indicates an illegal byte sequence.
EINPROGRESS Indicates that an operation is already in progress.
EINTR Indicates that a function call was interrupted.
EINVAL Indicates that an invalid argument was provided.
EIO Indicates an otherwise unspecified I/O error.
EISCONN Indicates that the socket is connected.
EISDIR Indicates that the path is a directory.
ELOOP Indicates too many levels of symbolic links in a path.
EMFILE Indicates that there are too many open files.
EMLINK Indicates that there are too many hard links to a file.
EMSGSIZE Indicates that the provided message is too long.
EMULTIHOP Indicates that a multihop was attempted.
ENAMETOOLONG Indicates that the filename is too long.
ENETDOWN Indicates that the network is down.
ENETRESET Indicates that the connection has been aborted by the network.
ENETUNREACH Indicates that the network is unreachable.
ENFILE Indicates too many open files in the system.
ENOBUFS Indicates that no buffer space is available.
ENODATA Indicates that no message is available on the stream head read queue.
ENODEV Indicates that there is no such device.
ENOENT Indicates that there is no such file or directory.
ENOEXEC Indicates an exec format error.
ENOLCK Indicates that there are no locks available.
ENOLINK Indications that a link has been severed.
ENOMEM Indicates that there is not enough space.
ENOMSG Indicates that there is no message of the desired type.
ENOPROTOOPT Indicates that a given protocol is not available.
ENOSPC Indicates that there is no space available on the device.
ENOSR Indicates that there are no stream resources available.
ENOSTR Indicates that a given resource is not a stream.
ENOSYS Indicates that a function has not been implemented.
ENOTCONN Indicates that the socket is not connected.
ENOTDIR Indicates that the path is not a directory.
ENOTEMPTY Indicates that the directory is not empty.
ENOTSOCK Indicates that the given item is not a socket.
ENOTSUP Indicates that a given operation is not supported.
ENOTTY Indicates an inappropriate I/O control operation.
ENXIO Indicates no such device or address.
EOPNOTSUPP Indicates that an operation is not supported on the socket. Note that while ENOTSUP and EOPNOTSUPP have the same value on Linux, according to POSIX.1 these error values should be distinct.)
EOVERFLOW Indicates that a value is too large to be stored in a given data type.
EPERM Indicates that the operation is not permitted.
EPIPE Indicates a broken pipe.
EPROTO Indicates a protocol error.
EPROTONOSUPPORT Indicates that a protocol is not supported.
EPROTOTYPE Indicates the wrong type of protocol for a socket.
ERANGE Indicates that the results are too large.
EROFS Indicates that the file system is read only.
ESPIPE Indicates an invalid seek operation.
ESRCH Indicates that there is no such process.
ESTALE Indicates that the file handle is stale.
ETIME Indicates an expired timer.
ETIMEDOUT Indicates that the connection timed out.
ETXTBSY Indicates that a text file is busy.
EWOULDBLOCK Indicates that the operation would block.
EXDEV Indicates an improper link.

Windows 系统特有的错误常量#

中英对照提交修改

下面的错误码与Windows系统相关:

常量 描述
WSAEINTR 表明中断的函数调用 .
WSAEBADF 表明一个无效的文件句柄 .
WSAEACCES 表明权限不够完成操作 .
WSAEFAULT 表明无效的指针地址 .
WSAEINVAL 表明无效的参数被传递 .
WSAEMFILE 表明有太多打开的文件 .
WSAEWOULDBLOCK 表明资源暂时不可用 .
WSAEINPROGRESS 表明操作当前正在进行中 .
WSAEALREADY 表明操作已经在进行中 .
WSAENOTSOCK 表明资源不是 socket.
WSAEDESTADDRREQ 表明需要目的地址 .
WSAEMSGSIZE 表明消息太长 .
WSAEPROTOTYPE 表明socket协议类型错误 .
WSAENOPROTOOPT 表明错误的协议选项 .
WSAEPROTONOSUPPORT 表明协议不被支持 .
WSAESOCKTNOSUPPORT 表明socket类型不被支持 .
WSAEOPNOTSUPP 表明操作不被支持 .
WSAEPFNOSUPPORT 表明协议簇不被支持 .
WSAEAFNOSUPPORT 表明地址簇不被支持 .
WSAEADDRINUSE 表明网络地址已经在使用 .
WSAEADDRNOTAVAIL 表明网络地址不可用.
WSAENETDOWN 表明网络关闭 .
WSAENETUNREACH 表明网络不可达 .
WSAENETRESET 表明网络连接被重置 .
WSAECONNABORTED 表明连接被终止 .
WSAECONNRESET 表明连接被同伴重置 .
WSAENOBUFS 表明没有可用的缓存空间 .
WSAEISCONN 表明socket已经连接 .
WSAENOTCONN 表明socket没有连接 .
WSAESHUTDOWN 表明数据在socket关闭之后,不能被发送 .
WSAETOOMANYREFS 表明有太多的引用 .
WSAETIMEDOUT 表明连接超时 .
WSAECONNREFUSED 表明连接被拒绝 .
WSAELOOP 表明名字不能被翻译 .
WSAENAMETOOLONG 表明名字太长 .
WSAEHOSTDOWN 表明网络主机关闭 .
WSAEHOSTUNREACH 表明没有到网络主机的路由 .
WSAENOTEMPTY 表明目录非空 .
WSAEPROCLIM 表明有太多的进程 .
WSAEUSERS 表明已经超过用户指标 .
WSAEDQUOT 表明已经超过磁盘指标 .
WSAESTALE 表明一个稳定的文件句柄引用 .
WSAEREMOTE 表明项目是远程的 .
WSASYSNOTREADY 表明网络子系统尚未准备好 .
WSAVERNOTSUPPORTED 表明 winsock.dll 版本在范围之外.
WSANOTINITIALISED 表明成功的 WSAStartup(Windows异步socket)还没有被执行 .
WSAEDISCON 表明一个优雅的关机正在进行 .
WSAENOMORE 表明没有更多的结果 .
WSAECANCELLED 表明一个操作已经被取消 .
WSAEINVALIDPROCTABLE 表明过程调用表是无效的 .
WSAEINVALIDPROVIDER 表明无效的服务提供者 .
WSAEPROVIDERFAILEDINIT 表明服务提供者初始化失败 .
WSASYSCALLFAILURE 表明系统调用失败 .
WSASERVICE_NOT_FOUND 表明服务没有被找到 .
WSATYPE_NOT_FOUND 表明类类型没有被找到 .
WSA_E_NO_MORE 表明没有更多的结果 .
WSA_E_CANCELLED 表明调用被取消 .
WSAEREFUSED 表明数据库请求被拒绝 .

dlopen Constants#

中英对照提交修改

如果在操作系统上可用,则以下常量在os.constants.dlopen中导出。 有关详细信息,请参阅dlopen(3)信息。

Constant Description
RTLD_LAZY Perform lazy binding. Node.js sets this flag by default.
RTLD_NOW Resolve all undefined symbols in the library before dlopen(3) returns.
RTLD_GLOBAL Symbols defined by the library will be made available for symbol resolution of subsequently loaded libraries.
RTLD_LOCAL The converse of `RTLD_GLOBAL`. This is the default behavior if neither flag is specified.
RTLD_DEEPBIND Make a self-contained library use its own symbols in preference to symbols from previously loaded libraries.

Priority Constants#

暂无中英对照提交修改

The following process scheduling constants are exported by os.constants.priority:

Constant Description
PRIORITY_LOW The lowest process scheduling priority. This corresponds to IDLE_PRIORITY_CLASS on Windows, and a nice value of 19 on all other platforms.
PRIORITY_BELOW_NORMAL The process scheduling priority above PRIORITY_LOW and below PRIORITY_NORMAL. This corresponds to BELOW_NORMAL_PRIORITY_CLASS on Windows, and a nice value of 10 on all other platforms.
PRIORITY_NORMAL The default process scheduling priority. This corresponds to NORMAL_PRIORITY_CLASS on Windows, and a nice value of 0 on all other platforms.
PRIORITY_ABOVE_NORMAL The process scheduling priority above PRIORITY_NORMAL and below PRIORITY_HIGH. This corresponds to ABOVE_NORMAL_PRIORITY_CLASS on Windows, and a nice value of -7 on all other platforms.
PRIORITY_HIGH The process scheduling priority above PRIORITY_ABOVE_NORMAL and below PRIORITY_HIGHEST. This corresponds to HIGH_PRIORITY_CLASS on Windows, and a nice value of -14 on all other platforms.
PRIORITY_HIGHEST The highest process scheduling priority. This corresponds to REALTIME_PRIORITY_CLASS on Windows, and a nice value of -20 on all other platforms.

libuv 常量#

中英对照提交修改

常量 描述
UV_UDP_REUSEADDR