This codemod validates and converts invalid argument types to fs.existsSync(). It's useful to migrate code that passes invalid argument types which now causes deprecation warnings or errors.
Starting with Node.js, passing invalid argument types to fs.existsSync() triggers a deprecation warning (DEP0187). The function should only receive string, Buffer, or URL arguments as documented in the Node.js fs.existsSync() documentation.
This codemod automatically:
- Validates that
fs.existsSync()receives valid argument types - Converts invalid argument types to valid ones where possible
- Handles both CommonJS (
require) and ESM (import) syntax - Adds type checks or conversions to ensure argument validity
Before:
const fs = require("node:fs");
const exists = fs.existsSync(123);After:
const fs = require("node:fs");
const exists = fs.existsSync(String(123));Before:
const fs = require("node:fs");
function checkFile(path) {
return fs.existsSync(path);
}After:
const fs = require("node:fs");
function checkFile(path) {
if (typeof path !== 'string' && !Buffer.isBuffer(path) && !(path instanceof URL)) {
path = String(path);
}
return fs.existsSync(path);
}Before:
const fs = require("node:fs");
const fileExists = fs.existsSync(null);After:
const fs = require("node:fs");
const fileExists = fs.existsSync(String(null || ''));Before:
import { existsSync } from "node:fs";
const exists = existsSync({ path: '/some/file' });After:
import { existsSync } from "node:fs";
const exists = existsSync(String({ path: '/some/file' }));