# DeprecationWarning: Passing invalid argument types to fs.existsSync is deprecated
Node.js でファイルシステム操作を行う際に、fs.existsSync に無効な引数型を渡すと以下のような警告が表示されることがあります。
(node:12345) [DEP0005] DeprecationWarning: Passing invalid argument types to fs.existsSync is deprecated and will throw an error in future versions of Node.js.
この警告の原因と解決方法について解説します。
# 警告が発生する原因
fs.existsSync は、ファイルやディレクトリが存在するかを同期的にチェックする関数です。この関数は文字列型(パス)を引数として期待していますが、以下のような無効な型を渡すと警告が発生します:
undefinednull- 数値型
- オブジェクト型
- その他の非文字列型
# よくある発生パターン
# パターン1: 変数が undefined の場合
const fs = require('fs');
// 変数が undefined の可能性がある
const filePath = process.env.FILE_PATH; // undefined の場合がある
// 警告が発生
if (fs.existsSync(filePath)) {
console.log('ファイルが存在します');
}
# パターン2: オブジェクトのプロパティが undefined の場合
const fs = require('fs');
const config = {
// path プロパティが存在しない
};
// 警告が発生
if (fs.existsSync(config.path)) {
console.log('ファイルが存在します');
}
# パターン3: 数値を渡してしまう場合
const fs = require('fs');
const fileId = 12345;
// 警告が発生(数値を文字列に変換していない)
if (fs.existsSync(fileId)) {
console.log('ファイルが存在します');
}
# 解決方法
# 方法1: 引数の型チェックを追加する
引数が文字列型であることを確認してから fs.existsSync を呼び出します。
const fs = require('fs');
function safeExistsSync(path) {
if (typeof path !== 'string' && path !== null && path !== undefined) {
return false;
}
if (path === null || path === undefined) {
return false;
}
return fs.existsSync(path);
}
// 使用例
const filePath = process.env.FILE_PATH;
if (safeExistsSync(filePath)) {
console.log('ファイルが存在します');
}
# 方法2: オプショナルチェーンとデフォルト値を使用する
const fs = require('fs');
const filePath = process.env.FILE_PATH || '';
if (filePath && fs.existsSync(filePath)) {
console.log('ファイルが存在します');
}
# 方法3: try-catch でエラーハンドリングする
const fs = require('fs');
function checkFileExists(path) {
try {
if (typeof path !== 'string' || !path) {
return false;
}
return fs.existsSync(path);
} catch (error) {
return false;
}
}
// 使用例
const filePath = process.env.FILE_PATH;
if (checkFileExists(filePath)) {
console.log('ファイルが存在します');
}
# 方法4: より厳密な型チェック(推奨)
const fs = require('fs');
const path = require('path');
function isValidPath(value) {
return typeof value === 'string' && value.trim().length > 0;
}
function safeExistsSync(filePath) {
if (!isValidPath(filePath)) {
return false;
}
try {
return fs.existsSync(filePath);
} catch (error) {
console.error('ファイルチェック中にエラーが発生しました:', error);
return false;
}
}
// 使用例
const filePath = process.env.FILE_PATH;
if (safeExistsSync(filePath)) {
console.log('ファイルが存在します');
}
# 実際のコード例
# Before(警告が発生するコード)
const fs = require('fs');
function loadConfig(configPath) {
if (fs.existsSync(configPath)) { // configPath が undefined の可能性
const config = require(configPath);
return config;
}
return {};
}
# After(修正後のコード)
const fs = require('fs');
function loadConfig(configPath) {
// 型チェックを追加
if (typeof configPath === 'string' && configPath && fs.existsSync(configPath)) {
const config = require(configPath);
return config;
}
return {};
}
# Node.js のバージョンによる動作の違い
- Node.js v20 以前: 警告が表示されるが、処理は続行される
- Node.js v21 以降: 将来的にはエラーがスローされる可能性がある(非推奨警告のため)
そのため、早めに対応することが推奨されます。
# 実装時の注意点
fs.existsSync の非推奨警告を回避するには:
- 引数の型チェック:
typeofを使用して文字列型であることを確認 - null/undefined チェック: 値が存在することを確認
- エラーハンドリング: try-catch で予期しないエラーに対応
- ヘルパー関数の作成: 再利用可能な安全な関数を作成
これらの対策を実装することで、警告を解消し、将来の Node.js バージョンでも安全に動作するコードを書ける。