Registry / serialization / to-vfile

to-vfile

JSON →
library8.0.0jsnpmunverified

to-vfile is a utility library for working with the file system using `vfile` instances, which represent virtual files in a processing pipeline. Unlike `vfile` itself, which primarily focuses on the file's content and metadata, `to-vfile` prioritizes interaction with the actual file system, providing asynchronous and synchronous methods to read from and write to disk. The current stable version is 8.0.0, requiring Node.js 16 or higher. The package sees active development, with major releases introducing breaking changes related to Node.js version support, module system (ESM-only since v7), and API structure. It is designed for scenarios where a file system is explicitly present and direct file operations are needed, complementing the `vfile` ecosystem by bridging virtual files with physical storage.

npm install to-vfile
INSTALL
IMPORT
SIG · TO-VFILE
T
to-vfile
serializationjavascriptv8.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

toVFile
import { toVFile } from 'to-vfile'
const toVFile = require('to-vfile')
Package is ESM-only since v7.0.0. Use `import` statements. `toVFile` is a named export, not a default export.
read
import { read } from 'to-vfile'
import toVFile from 'to-vfile'; await toVFile.read('file.txt')
Since v8.0.0, `read`, `write`, `readSync`, and `writeSync` are top-level named exports and no longer methods on the `toVFile` function.
writeSync
import { writeSync } from 'to-vfile'
const { writeSync } = require('to-vfile')
This package is ESM-only since v7.0.0. Named exports must be imported using `import` statements.

Demonstrates creating virtual files from paths and URLs, asynchronously reading file contents with and without encoding, modifying the content, and writing it back to the file system using named exports.

import { toVFile, read, write } from 'to-vfile'; import { resolve } from 'path'; import { fileURLToPath } from 'url'; import { promises as fs } from 'fs'; const __dirname = fileURLToPath(new URL('.', import.meta.url)); const testFilePath = resolve(__dirname, 'test-file.md'); async function runExample() { // Create a dummy file for reading and writing await fs.writeFile(testFilePath, '# Hello from to-vfile!\nThis is a test file.'); // Create a VFile from a path console.log('VFile from path:', toVFile('readme.md')); console.log('VFile from URL:', toVFile(new URL('readme.md', import.meta.url))); // Read a file asynchronously const headFile = await read(testFilePath); console.log('\nRead file content (Buffer):', headFile.value); // Read a file asynchronously with encoding const headFileUtf8 = await read(testFilePath, 'utf8'); console.log('Read file content (UTF-8 string):', headFileUtf8.value); // Modify and write the file back headFileUtf8.value = String(headFileUtf8.value) + '\nAppended content.\n'; const writtenFile = await write(headFileUtf8, { encoding: 'utf8' }); console.log('\nFile written, new content:', String(writtenFile.value)); // Clean up the dummy file await fs.unlink(testFilePath); } runExample().catch(console.error);
Debug
Known issues
breakingVersion 8.0.0 changed the minimum Node.js requirement to Node.js 16. Older Node.js versions are no longer supported.
fix
Update your Node.js environment to version 16 or newer.
affects: >=8.0.0
breakingIn version 8.0.0, methods like `read()`, `write()`, `readSync()`, and `writeSync()` were removed from the `toVFile` function. They are now standalone named exports.
fix
Instead of `toVFile.read()`, use `import { read } from 'to-vfile'; await read()`. Similarly for `write` and their synchronous counterparts.
affects: >=8.0.0
breakingVersion 7.0.0 switched the package to be ESM-only (ECMAScript Modules). CommonJS `require()` syntax is no longer supported.
fix
Migrate your project to use ESM `import` statements. Ensure your `package.json` has `"type": "module"` or use `.mjs` file extensions for files importing `to-vfile`.
affects: >=7.0.0
breakingVersion 8.0.0 changed to use an `export` map. This primarily affects users who might have been relying on internal or private APIs, which are now strictly hidden.
fix
Ensure you are only importing and using documented public APIs. Do not access files directly from the package's internal structure.
affects: >=8.0.0
breakingThe `Mode` type was removed in version 8.0.0. If you were using it for file permissions, you should now rely on Node.js's built-in types for file modes.
fix
Import `Mode` or related file system types directly from Node.js's type definitions if needed, e.g., `@types/node`.
affects: >=8.0.0
Errors
Common errors & fixes
TypeError: require is not a function
Attempting to import `to-vfile` using CommonJS `require()` in an ESM context, or when `to-vfile` is an ESM-only package.
fix
Change `const { toVFile } = require('to-vfile')` to `import { toVFile } from 'to-vfile'`. Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`).
TypeError: toVFile.read is not a function
Using the deprecated `toVFile.read()` method pattern after upgrading to `to-vfile` v8.0.0 or higher.
fix
The `read`, `write`, `readSync`, and `writeSync` functions are now top-level named exports. Use `import { read } from 'to-vfile'` and call `read()` directly.
ERR_MODULE_NOT_FOUND: Cannot find package 'to-vfile' imported from ...
This error can occur in Node.js when attempting to import an ESM-only package without proper `package.json` configuration or using an outdated Node.js version.
fix
Verify Node.js is v16+, `package.json` has `"type": "module"`, or the importing file has a `.mjs` extension. Also, ensure the package is installed correctly (`npm install to-vfile`).
Upgrade
Version history
8.0.0latest on npm
Audit
Dependencies
vfilerequiredCore dependency for creating and manipulating virtual files within the processing pipeline.
Agent activity
35 hits · last 30 days
node
28
OpenAI (training)
2
Resources
to-vfile — npm install to-vfile · libregistry