{"id":12956,"library":"chmodrp","title":"Recursive Chmod with Promises","description":"`chmodrp` is a lightweight utility package for Node.js that provides a recursive `chmod` functionality, mirroring the behavior of the `chmod -R` command-line operation. It exposes this capability through a modern, Promise-based API, simplifying asynchronous file permission management. The current stable version is 1.0.2, and as a focused utility, it typically follows an infrequent release cadence, with updates primarily for maintenance or minor enhancements. Its core differentiator is the ergonomic Promise interface, which integrates seamlessly with `async/await` patterns, offering a significant improvement over traditional callback-based `fs` methods for ensuring consistent file and directory permissions in build systems, deployment scripts, and automated administrative tasks.","status":"active","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/leonzalion/chmodrp","tags":["javascript","chmod","chmodr","recursive","promise","fs"],"install":[{"cmd":"npm install chmodrp","lang":"bash","label":"npm"},{"cmd":"yarn add chmodrp","lang":"bash","label":"yarn"},{"cmd":"pnpm add chmodrp","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package is ESM-only since its initial release, requiring `import` syntax. CommonJS `require()` will not work directly.","wrong":"const chmodrp = require('chmodrp');","symbol":"chmodrp","correct":"import chmodrp from 'chmodrp';"}],"quickstart":{"code":"import chmodrp from 'chmodrp';\nimport { mkdtemp, writeFile, mkdir } from 'node:fs/promises';\nimport { join } from 'node:path';\nimport { tmpdir } from 'node:os';\n\nasync function setupAndChangePermissions() {\n  const baseTempDir = join(tmpdir(), 'chmodrp-example-');\n  const tempDir = await mkdtemp(baseTempDir);\n  const filePath1 = join(tempDir, 'file1.txt');\n  const subDirPath = join(tempDir, 'sub-dir');\n  const filePath2 = join(subDirPath, 'file2.txt');\n\n  console.log(`Created temporary base directory: ${tempDir}`);\n\n  // Create files and directories\n  await writeFile(filePath1, 'Content for file 1.');\n  await mkdir(subDirPath, { recursive: true });\n  await writeFile(filePath2, 'Content for file 2.');\n\n  console.log('Initial files and directories created.');\n  console.log('Changing permissions recursively to 0o755 (rwxr-xr-x)...');\n\n  try {\n    // Recursively change permissions for all files and directories within tempDir\n    await chmodrp(tempDir, 0o755);\n    console.log('Permissions changed successfully for:', tempDir);\n  } catch (error) {\n    console.error('Failed to change permissions:', error);\n    process.exit(1);\n  }\n\n  // In a real application, you might verify permissions here or proceed with cleanup.\n  // For cleanup, use 'rm -rf' or Node.js fs.rm (recursive: true) for Node 14+\n  // await rm(tempDir, { recursive: true, force: true }); // Node 14.14+\n  // console.log('Cleaned up temporary directory.');\n}\n\nsetupAndChangePermissions().catch(console.error);","lang":"typescript","description":"This example demonstrates how to use `chmodrp` to recursively set file and directory permissions within a temporary directory, showing basic setup and error handling."},"warnings":[{"fix":"Always use octal literal notation (e.g., `0o755`) or parse strings to integers (`parseInt('755', 8)`) for the permission `mode`.","message":"The `mode` argument expects an octal number (e.g., `0o755`) or an integer representing the permission. Passing a string like '755' will lead to incorrect permissions or a `TypeError`.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure the user running the Node.js process has appropriate write and execute permissions on the target directory and its files. This may involve using `sudo` or adjusting file ownership/permissions manually beforehand.","cause":"The Node.js process does not have sufficient operating system permissions to change permissions on the specified path or its contents.","error":"EACCES: permission denied, chmod"},{"fix":"Correct the mode argument to an octal number, e.g., change `'755'` to `0o755`.","cause":"The permission mode was provided as a string instead of a numerical value (octal or integer).","error":"TypeError: The \"mode\" argument must be of type number. Received type string ('755')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}