[Node.js]実行時のカレントディレクトリ(フォルダ)を取得する

スクリプトが実行されたディレクトリを取得するには process.cwd() を使用します。

カレントディレクトリを取得する方法

カレントディレクトリ(フルパス) = process.cwd();

サンプルコード

let currentWorkingDirectory = process.cwd();
console.log(currentWorkingDirectory);

イメージ

D:\test>node .\sample.js
D:\test

D:\test>cd ..

D:\>node .\test\sample.js
D:\

検証環境

関連ページ