process.cwd()方法是流程模塊的內置應用程序編程接口,用於獲取node.js流程的當前工作目錄。
用法:
process.cwd()
參數:此方法不接受任何參數。
返回值:此方法返回一個字符串,該字符串指定node.js進程的當前工作目錄。
以下示例說明了Node.js中process.cwd()方法的使用:
範例1:
// Node.js program to demonstrate the
// process.cwd() Method
// Include process module
const process = require('process');
// Printing current directory
console.log("Current working directory:",
process.cwd());
輸出:
Current working directory:C:\nodejs\g\process
範例2:
// Node.js program to demonstrate the
// process.cwd() Method
// Include process module
const process = require('process');
// Printing current directory
console.log("Current working directory:",
process.cwd());
try {
// Change working directory
process.chdir('../');
console.log("Working directory after changing"
+ " directory:", process.cwd());
} catch (err) {
// printing error if occurs
console.error("error occured while changing"
+ " directory:", err);
}
輸出:
Current working directory:C:\nodejs\g\process Working directory after changing directory:C:\nodejs\g\os
注意:上麵的程序將通過使用node filename.js
命令。
參考:
https://nodejs.org/api/process.html#process_process_cwd
相關用法
注:本文由純淨天空篩選整理自anwesha0107大神的英文原創作品 Node.js | process.cwd() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。