filenamify
Convert a string or path into a valid filename by replacing reserved and control characters.
Official Documentation ↗v7.0.3. Check the official docs for the latest API surface.Installation
npm install filenamifypnpm add filenamifyMethods
No methods match this filter.
filenamify turns untrusted strings into filenames that every mainstream filesystem accepts. It is typically applied to user-supplied download names and generated report titles.
Paths versus single names
The default export sanitizes one filename. filenamify.path preserves directory separators, so uploads/report: Q1.txt keeps its directory while the leaf name is repaired.
Limits
Filesystems impose different length limits. Use maxLength to truncate, and append a short hash when two truncated names could collide.
Security
Official security statements captured from the package documentation:
- 官方 README 声明会移除不可打印控制字符(含 Unicode 双向标记)并归一化 Unicode 空白。 — “This module also removes non-printable control characters (including Unicode bidirectional marks) and normalizes Unicode whitespace.” (source: https://github.com/sindresorhus/filenamify/blob/v7.0.3/readme.md, fetched 2026-11-08T17:41:29+08:00)
- 官方 d.ts 声明 replacement 不得包含保留字符;若结果仍为 Windows 保留名则改用 ‘!’。 — “Cannot contain:
<>:"/\|?*or control characters. If the replacement would leave the result as a Windows reserved name,!is used instead.” (source: https://cdn.jsdelivr.net/npm/[email protected]/filenamify.d.ts, fetched 2026-11-08T17:41:29+08:00)
API reference evidence
Fetched from official sources on 2026-11-08T17:41:29+08:00 via PowerShell Invoke-WebRequest 下载官方仓库 v7.0.3 的 readme.md 与 index.d.ts(raw.githubusercontent.com/sindresorhus/filenamify/v7.0.3),并从 cdn.jsdelivr.net/npm/[email protected] 取 filenamify.d.ts、filenamify-path.d.ts 原文.
filenamify→ signature from https://cdn.jsdelivr.net/npm/[email protected]/filenamify.d.ts; official text: “export default function filenamify(string: string, options?: Options): string;”filenamify.path→ signature from https://cdn.jsdelivr.net/npm/[email protected]/filenamify.d.ts; official text: “export default function filenamify(string: string, options?: Options): string;”
FAQ
- Which characters does filenamify replace?
- Characters that are reserved on Windows, macOS, or Linux, plus control characters. It also collapses leading dots so a name cannot become a hidden file.
- How do I control the replacement character?
- Pass the replacement option, for example { replacement: '-' }. The default is an exclamation mark, which keeps the transformed name recognizable.
- Does it handle existing file paths?
- Use filenamify.path instead of the default export. It keeps the directory separators and sanitizes each segment, so a nested path stays a nested path.
- Can I limit the filename length?
- Yes. The maxLength option truncates the result; combine it with a hash suffix when truncated names must remain unique.
Report a problem with this API docOpens a pre-filled issue on GitHub.