Registry / storage / file-fetch

file-fetch

JSON →
library2.0.1jsnpmunverified

A nodeify-fetch compatible fetch implementation for reading and writing to the local file system using file: URLs and URIs, including implicit relative paths. v2.0.1 (stable, low release cadence). Supports GET for reading content as stream, text, or JSON, and PUT for writing from string or Readable stream. Key differentiators: supports file: URIs (not just absolute file:/// URLs), custom baseURL and contentType factory, and streaming body. No external dependencies beyond Node.js built-ins.

npm install file-fetch
INSTALL
IMPORT
SIG · FILE-FETCH
F
file-fetch
storagejavascriptv2.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (fetch)
import fetch from 'file-fetch'
const fetch = require('file-fetch')
ESM-only package; CJS require will fail or require() can be used for dynamic import: const fetch = await import('file-fetch')
factory
import { factory } from 'file-fetch'
import factory from 'file-fetch/factory'
Named export, not a separate subpath export. Use named import from main module.
fetch (default) combined with factory
import fetch, { factory } from 'file-fetch'
Both the default fetch function and the factory function can be imported in a single statement since v2.0.0.

Demonstrates reading a file as text, writing with PUT, and parsing JSON. Uses file: URLs and relative paths.

import fetch from 'file-fetch' async function main() { // Read a file as text const res = await fetch(new URL('example.txt', import.meta.url)) console.log(await res.text()) // Write to file using PUT await fetch('file:///tmp/example.log', { method: 'PUT', body: 'Hello, file-fetch!' }) // Read JSON file const jsonRes = await fetch(new URL('./data.json', import.meta.url)) const data = await jsonRes.json() console.log(data) } main().catch(console.error)
Debug
Known issues
breakingv2.0.0 dropped support for CommonJS require; package is now ESM-only.
fix
Use import statements or dynamic import() instead of require().
affects: >=2.0.0
gotchaAbsolute file: URLs must start with file:/// (three slashes). WRONG: file://tmp/ or file:/tmp/
fix
Always use file:/// prefix for absolute paths. Example: file:///home/user/file.txt
affects: >=1.0.0
gotchaThe response body is a Readable stream only for GET requests; for PUT, the return is a different response object (no body).
fix
Check method: if method is PUT, do not attempt to read response body.
affects: >=1.0.0
deprecatedCreating a custom fetch with factory() does not support a custom 'fetch' like the standard fetch, but uses internal implementation.
fix
Use factory() if you need custom baseURL or contentType; for other customizations, consider a wrapper.
affects: >=2.0.0
Errors
Common errors & fixes
Error: Not supported
Using a URL that is not a file: scheme or a relative path/URI that cannot be resolved to a file.
fix
Ensure the URL or URI is a valid file: URL (file:///...), file: URI (file:path), or a relative path. For absolute URLs, must start with file:///.
TypeError: Cannot read properties of undefined (reading 'pipe')
Attempting to access res.body on a PUT response which has no body stream.
fix
Only read res.body for GET requests. For PUT, the response does not include a body.
Error: ENOENT: no such file or directory, open '...'
The requested file does not exist.
fix
Verify the file path exists. Use absolute URLs or relative paths with correct baseURL.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
readable-streamrequiredused internally for stream handling (but also exposed for user convenience)
nodeify-fetchoptionalprovides the fetch specification interface that file-fetch is compatible with
Agent activity
21 hits · last 30 days
node
18
Amazon
1
OpenAI (training)
1
Resources
file-fetch — npm install file-fetch · libregistry