# filenamify

> Convert a string to a valid safe filename

Latest version **7.0.3** (published 2026-08-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install filenamify
pnpm add filenamify
yarn add filenamify
bun add filenamify
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 7.0.3 |
| Published | 2026-08-28 |
| First published | 2015-01-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 1 |
| Unpacked size | 12.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 525 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | filename, safe, sanitize, file, name, string, path, filepath, convert, valid, dirname |

## Links

- npm: https://www.npmjs.com/package/filenamify
- Repository: https://github.com/sindresorhus/filenamify
- Homepage: https://github.com/sindresorhus/filenamify#readme
- Issues: https://github.com/sindresorhus/filenamify/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/filenamify

## Dependencies (1)

- [filename-reserved-regex](https://npm.io/package/filename-reserved-regex.md) ^4.0.1

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 7.0.3 (latest) — 2026-08-28
- 7.0.2 — 2026-06-16
- 7.0.1 — 2025-11-05
- 7.0.0 — 2025-09-14
- 6.0.0 — 2023-04-23
- 5.1.1 — 2022-04-27
- 5.1.0 — 2022-01-06
- 5.0.2 — 2021-10-17
- 5.0.1 — 2021-08-26
- 5.0.0 — 2021-07-05
- 4.3.0 — 2021-04-26
- 4.2.0 — 2020-09-24
- 4.1.0 — 2019-05-30
- 4.0.0 — 2019-03-31
- 3.0.0 — 2019-03-07
- … 7 more at https://npm.io/package/filenamify/versions

## README

# filenamify

> Convert a string to a valid safe filename

On Unix-like systems, `/` is reserved. On Windows, [`<>:"/\|?*`](http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29#naming_conventions) along with trailing periods and spaces are reserved.

This module also removes non-printable control characters (including Unicode bidirectional marks) and normalizes Unicode whitespace.

## Install

```sh
npm install filenamify
```

## Usage

```js
import filenamify from 'filenamify';

filenamify('<foo/bar>');
//=> '!foo!bar!'

filenamify('foo:"bar"', {replacement: '🐴'});
//=> 'foo🐴bar🐴'
```

## API

### filenamify(string, options?)

Convert a string to a valid filename.

### filenamifyPath(path, options?)

Convert the filename in a path to a valid filename and return the augmented path.

```js
import {filenamifyPath} from 'filenamify';

filenamifyPath('foo:bar');
//=> 'foo!bar'
```

#### options

Type: `object`

##### replacement

Type: `string`\
Default: `'!'`

String to use as replacement for reserved filename characters.

Cannot contain: `<` `>` `:` `"` `/` `\` `|` `?` `*` or control characters.

If the replacement would leave the result as a Windows reserved name, `!` is used instead.

##### maxLength

Type: `number`\
Default: `100`

Truncate the filename to the given length.

Only the base of the filename is truncated, preserving the extension. If the extension itself is longer than `maxLength`, you will get a string that is longer than `maxLength`, so you need to check for that if you allow arbitrary extensions.

The result may also exceed `maxLength` when a suffix is needed to avoid a Windows reserved name.

Truncation is grapheme-aware and will not split Unicode characters (surrogate pairs or extended grapheme clusters). If the remaining budget (after accounting for the extension) is smaller than a whole grapheme, the base filename may be truncated to an empty string to avoid splitting.

Systems generally allow up to 255 characters, but we default to 100 for usability reasons.

## Browser-only import

You can also import `filenamify/browser`, which only imports `filenamify` and not `filenamifyPath`, which relies on `path` being available or polyfilled. Importing `filenamify` this way is therefore useful when it is shipped using `webpack` or similar tools, and if `filenamifyPath` is not needed.

```js
import filenamify from 'filenamify/browser';

filenamify('<foo/bar>');
//=> '!foo!bar!'
```

## Related

- [filenamify-cli](https://github.com/sindresorhus/filenamify-cli) - CLI for this module
- [filenamify-url](https://github.com/sindresorhus/filenamify-url) - Convert a URL to a valid filename
- [valid-filename](https://github.com/sindresorhus/valid-filename) - Check if a string is a valid filename
- [unused-filename](https://github.com/sindresorhus/unused-filename) - Get a unused filename by appending a number if it exists
- [slugify](https://github.com/sindresorhus/slugify) - Slugify a string

---
_Source: https://npm.io/package/filenamify · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
