{"library":"mv","title":"mv (Node.js)","description":"The `mv` package by `andrewrk` provides a robust utility for moving files and directories in Node.js, specifically addressing the limitation of `fs.rename` which cannot perform operations across different devices or file systems. It first attempts a standard `fs.rename` and, upon failure (e.g., cross-device boundary), transparently falls back to a copy-then-unlink strategy. For files, this involves piping data, and for directories, it utilizes a recursive copy (`ncp`) followed by removal of the source (`rimraf`). It supports options like automatically creating destination parent directories (`mkdirp: true`) and preventing overwrites (`clobber: false`). The current stable version, `2.1.1`, was released in 2015. Given its age and lack of recent updates, this package is considered abandoned. Developers are advised to consider actively maintained alternatives, such as `move-file`, which offer modern Promise-based APIs and enhanced ESM support, if possible.","language":"javascript","status":"abandoned","last_verified":"Sun Apr 19","install":{"commands":["npm install mv"],"cli":null},"imports":["const mv = require('mv');","import mv from 'mv';","const mv = await import('mv').then(mod => mod.default || mod);"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const mv = require('mv');\nconst fs = require('fs');\nconst path = require('path');\n\nasync function runExample() {\n  const sourceFile = path.join(__dirname, 'source.txt');\n  const destFile = path.join(__dirname, 'dest', 'new-name.txt');\n  const sourceDir = path.join(__dirname, 'old-dir');\n  const destDir = path.join(__dirname, 'target', 'new-dir');\n\n  // Ensure directories exist for testing\n  fs.mkdirSync(path.join(__dirname, 'dest'), { recursive: true });\n  fs.mkdirSync(sourceDir, { recursive: true });\n  fs.writeFileSync(sourceFile, 'Hello from source file!');\n  fs.writeFileSync(path.join(sourceDir, 'file-in-dir.txt'), 'Content in directory!');\n\n  console.log('Moving a file...');\n  await new Promise((resolve, reject) => {\n    mv(sourceFile, destFile, function(err) {\n      if (err) return reject(err);\n      console.log('File moved successfully to', destFile);\n      resolve();\n    });\n  });\n\n  console.log('\\nMoving a directory and creating parent path...');\n  await new Promise((resolve, reject) => {\n    mv(sourceDir, destDir, { mkdirp: true }, function(err) {\n      if (err) return reject(err);\n      console.log('Directory moved successfully to', destDir);\n      resolve();\n    });\n  });\n\n  const existingFile = path.join(__dirname, 'existing.txt');\n  fs.writeFileSync(existingFile, 'This file exists.');\n  const clashTarget = path.join(__dirname, 'existing.txt'); // Intentionally clash\n  fs.writeFileSync(clashTarget, 'This will be clobbered unless clobber:false.');\n\n  console.log('\\nAttempting to move file with clobber: false (expecting EEXIST error)...');\n  await new Promise((resolve) => {\n    mv(sourceFile, clashTarget, { clobber: false }, function(err) {\n      if (err) {\n        console.error('Expected error when clobber: false:', err.code, err.message);\n        // Clean up potentially clobbered file for next run if it wasn't the exact clash\n        if (fs.existsSync(clashTarget)) fs.unlinkSync(clashTarget);\n        resolve();\n      } else {\n        console.log('File moved without error when clobber: false (unexpected for this example).');\n        resolve();\n      }\n    });\n  }).catch(console.error);\n\n  // Clean up created files/dirs\n  if (fs.existsSync(sourceFile)) fs.unlinkSync(sourceFile);\n  if (fs.existsSync(destFile)) fs.unlinkSync(destFile);\n  if (fs.existsSync(sourceDir)) fs.rmSync(sourceDir, { recursive: true, force: true });\n  if (fs.existsSync(destDir)) fs.rmSync(destDir, { recursive: true, force: true });\n  if (fs.existsSync(path.join(__dirname, 'dest'))) fs.rmdirSync(path.join(__dirname, 'dest'));\n  if (fs.existsSync(path.join(__dirname, 'target'))) fs.rmdirSync(path.join(__dirname, 'target'));\n  if (fs.existsSync(existingFile)) fs.unlinkSync(existingFile);\n}\n\nrunExample().catch(console.error);\n","lang":"javascript","description":"Demonstrates moving a file, moving a directory (with `mkdirp`), and handling the `EEXIST` error when attempting to overwrite a file with `clobber: false`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}