字符串路径


【String paths】

字符串路径被解释为 UTF-8 字符序列,用于标识绝对或相对的文件名。相对路径将相对于当前工作目录解析,该目录由调用 process.cwd() 确定。

【String paths are interpreted as UTF-8 character sequences identifying the absolute or relative filename. Relative paths will be resolved relative to the current working directory as determined by calling process.cwd().】

在 POSIX 上使用绝对路径的示例:

【Example using an absolute path on POSIX:】

import { open } from 'node:fs/promises';

let fd;
try {
  fd = await open('/open/some/file.txt', 'r');
  // Do something with the file
} finally {
  await fd?.close();
} 

在 POSIX 上使用相对路径的示例(相对于 process.cwd()):

【Example using a relative path on POSIX (relative to process.cwd()):】

import { open } from 'node:fs/promises';

let fd;
try {
  fd = await open('file.txt', 'r');
  // Do something with the file
} finally {
  await fd?.close();
}