https://github.com/sindresorhus/file-url
  
  
    Convert a file path to a file URL: `unicorn.jpg` → `file:///Users/sindresorhus/unicorn.jpg` 
    https://github.com/sindresorhus/file-url
  
        Last synced: 7 months ago 
        JSON representation
    
Convert a file path to a file URL: `unicorn.jpg` → `file:///Users/sindresorhus/unicorn.jpg`
- Host: GitHub
 - URL: https://github.com/sindresorhus/file-url
 - Owner: sindresorhus
 - License: mit
 - Created: 2014-04-04T14:46:53.000Z (over 11 years ago)
 - Default Branch: main
 - Last Pushed: 2024-04-01T17:02:35.000Z (over 1 year ago)
 - Last Synced: 2025-03-29T05:24:28.466Z (7 months ago)
 - Language: JavaScript
 - Homepage:
 - Size: 20.5 KB
 - Stars: 61
 - Watchers: 5
 - Forks: 8
 - Open Issues: 1
 - 
            Metadata Files:
            
- Readme: readme.md
 - License: license
 - Security: .github/security.md
 
 
Awesome Lists containing this project
- awesome-nodejs - file-url - Convert a file path to a file URL: `unicorn.jpg` → `file:///Users/sindresorhus/unicorn.jpg`. (Repository / URL)
 
README
          # file-url
> Convert a file path to a file url: `unicorn.jpg` → `file:///Users/sindresorhus/unicorn.jpg`
> [!TIP]
> Node.js supports this built-in now with [`url.pathToFileURL(path)`](https://nodejs.org/api/url.html#urlpathtofileurlpath).
## Install
```sh
npm install file-url
```
## Usage
```js
import fileUrl from 'file-url';
fileUrl('unicorn.jpg');
//=> 'file:///Users/sindresorhus/dev/file-url/unicorn.jpg'
fileUrl('/Users/pony/pics/unicorn.jpg');
//=> 'file:///Users/pony/pics/unicorn.jpg'
fileUrl('unicorn.jpg', {resolve: false});
//=> 'file:///unicorn.jpg'
```
## API
### fileUrl(filePath, options?)
Returns the `filePath` converted to a file URL.
It does not support filenames with backslashes.
#### filePath
Type: `string`
The file path to convert.
#### options
Type: `object`
##### resolve
Type: `boolean`\
Default: `true`
Passing `false` will make it not call `path.resolve()` on the path.
## Related
- [file-url-cli](https://github.com/sindresorhus/file-url-cli) - CLI for this module