Migrate fs-extra to Node.js fs
org.openrewrite.node.migrate.fs-extra.migrate-fs-extra-to-node-fsArtifact@openrewrite/recipes-nodejsReplaces fs-extra with the Node.js standard library (node:fs, node:fs/promises, node:path). fs-extra's own methods (remove, ensureDir, pathExists, readJson, writeJson, outputFile, outputJson, ensureFile, copy, emptyDir) are rewritten to their native equivalents and the fs methods fs-extra re-exports are bound from node:fs or node:fs/promises directly. move becomes a call to a helper written into the same file, since rename alone doesn't create the destination's parent, refuse an existing destination, or work across devices. Calls without a faithful native equivalent (ensureSymlink, ensureLink, ...) are left in place and listed in the FsExtraManualMigrationSteps data table. fs-extra and @types/fs-extra are removed from each package.json whose sources no longer use it. Tests that mock or spy on fs-extra itself (vi.mock('fs-extra'), vi.spyOn(fs, 'outputFile'), ...) keep watching a module the migrated code no longer calls, so they fail until they mock node:fs / node:fs/promises instead. Every such site is listed in the data table; migrate them together with the code they cover.
Usage
This recipe has no required configuration options. You’ll need the Moderne CLI configured before running the command below.
mod run . --recipe org.openrewrite.node.migrate.fs-extra.migrate-fs-extra-to-node-fsIf the recipe isn’t available locally, install it with:
mod config recipes npm install @openrewrite/recipes-nodejsDefinition
This recipe runs the following recipes in order.
- Replace fs-extra
removewithfs.rmorg.openrewrite.node.migrate.fs-extra.replace-fs-extra-remove - Replace fs-extra
ensureDir/mkdirp/mkdirswithfs.mkdirorg.openrewrite.node.migrate.fs-extra.replace-fs-extra-mkdirs - Replace fs-extra
pathExistswithfs.access/fs.existsSyncorg.openrewrite.node.migrate.fs-extra.replace-fs-extra-path-exists - Replace fs-extra
readJsonwithJSON.parseofreadFileorg.openrewrite.node.migrate.fs-extra.replace-fs-extra-read-json - Replace fs-extra
writeJsonwithfs.writeFileorg.openrewrite.node.migrate.fs-extra.replace-fs-extra-write-json - Replace fs-extra
outputFileandoutputJsonwithmkdir+writeFileorg.openrewrite.node.migrate.fs-extra.replace-fs-extra-output-file - Replace fs-extra
ensureFilewithfs.mkdirandfs.writeFileorg.openrewrite.node.migrate.fs-extra.replace-fs-extra-ensure-file - Replace fs-extra
emptyDirwithmkdirandrmof each entryorg.openrewrite.node.migrate.fs-extra.replace-fs-extra-empty-dir - Replace fs-extra
movewith a generated helperorg.openrewrite.node.migrate.fs-extra.replace-fs-extra-move - Replace fs-extra
copywithfs.cporg.openrewrite.node.migrate.fs-extra.replace-fs-extra-copy - Replace fs-extra's re-exported
fsmethods withnode:fsorg.openrewrite.node.migrate.fs-extra.replace-fs-extra-native-methods - Find fs-extra usages that need manual migration
org.openrewrite.node.migrate.fs-extra.find-fs-extra-manual-migrations - Remove unused
fs-extradependencyorg.openrewrite.node.migrate.fs-extra.remove-unused-fs-extra-dependency