process.chdir(directory)
directory<string>
process.chdir() 方法会更改 Node.js 进程的当前工作目录,如果操作失败(例如指定的 directory 不存在),则会抛出异常。
🌐 The process.chdir() method changes the current working directory of the
Node.js process or throws an exception if doing so fails (for instance, if
the specified directory does not exist).
import { chdir, cwd } from 'node:process';
console.log(`Starting directory: ${cwd()}`);
try {
chdir('/tmp');
console.log(`New directory: ${cwd()}`);
} catch (err) {
console.error(`chdir: ${err}`);
}const { chdir, cwd } = require('node:process');
console.log(`Starting directory: ${cwd()}`);
try {
chdir('/tmp');
console.log(`New directory: ${cwd()}`);
} catch (err) {
console.error(`chdir: ${err}`);
}此功能在 Worker 线程中不可用。
🌐 This feature is not available in Worker threads.