UNPKG

3.48 kBMarkdownView Raw
1# memfs
2
3[![][chat-badge]][chat] [![][npm-badge]][npm-url] [![][travis-badge]][travis-url]
4
5In-memory file-system with [Node's `fs` API](https://nodejs.org/api/fs.html).
6
7- Node's `fs` API implemented, see [_old API Status_](./docs/api-status.md), [missing list](https://github.com/streamich/memfs/issues/735), [missing `opendir`](https://github.com/streamich/memfs/issues/663)
8- Stores files in memory, in `Buffer`s
9- Throws sameish\* errors as Node.js
10- Has concept of _i-nodes_
11- Implements _hard links_
12- Implements _soft links_ (aka symlinks, symbolic links)
13- Permissions may\* be implemented in the future
14- Can be used in browser, see [`memfs-webpack`](https://github.com/streamich/memfs-webpack)
15
16### Install
17
18```shell
19npm install --save memfs
20```
21
22## Usage
23
24```js
25import { fs } from 'memfs';
26
27fs.writeFileSync('/hello.txt', 'World!');
28fs.readFileSync('/hello.txt', 'utf8'); // World!
29```
30
31Create a file system from a plain JSON:
32
33```js
34import { fs, vol } from 'memfs';
35
36const json = {
37 './README.md': '1',
38 './src/index.js': '2',
39 './node_modules/debug/index.js': '3',
40};
41vol.fromJSON(json, '/app');
42
43fs.readFileSync('/app/README.md', 'utf8'); // 1
44vol.readFileSync('/app/src/index.js', 'utf8'); // 2
45```
46
47Export to JSON:
48
49```js
50vol.writeFileSync('/script.sh', 'sudo rm -rf *');
51vol.toJSON(); // {"/script.sh": "sudo rm -rf *"}
52```
53
54Use it for testing:
55
56```js
57vol.writeFileSync('/foo', 'bar');
58expect(vol.toJSON()).toEqual({ '/foo': 'bar' });
59```
60
61Create as many filesystem volumes as you need:
62
63```js
64import { Volume } from 'memfs';
65
66const vol = Volume.fromJSON({ '/foo': 'bar' });
67vol.readFileSync('/foo'); // bar
68
69const vol2 = Volume.fromJSON({ '/foo': 'bar 2' });
70vol2.readFileSync('/foo'); // bar 2
71```
72
73Use `memfs` together with [`unionfs`][unionfs] to create one filesystem
74from your in-memory volumes and the real disk filesystem:
75
76```js
77import * as fs from 'fs';
78import { ufs } from 'unionfs';
79
80ufs.use(fs).use(vol);
81
82ufs.readFileSync('/foo'); // bar
83```
84
85Use [`fs-monkey`][fs-monkey] to monkey-patch Node's `require` function:
86
87```js
88import { patchRequire } from 'fs-monkey';
89
90vol.writeFileSync('/index.js', 'console.log("hi world")');
91patchRequire(vol);
92require('/index'); // hi world
93```
94
95## Docs
96
97- [Reference](./docs/reference.md)
98- [Relative paths](./docs/relative-paths.md)
99- [API status](./docs/api-status.md)
100- [Dependencies](./docs/dependencies.md)
101
102## See also
103
104- [`spyfs`][spyfs] - spies on filesystem actions
105- [`unionfs`][unionfs] - creates a union of multiple filesystem volumes
106- [`linkfs`][linkfs] - redirects filesystem paths
107- [`fs-monkey`][fs-monkey] - monkey-patches Node's `fs` module and `require` function
108- [`libfs`](https://github.com/streamich/full-js/blob/master/src/lib/fs.ts) - real filesystem (that executes UNIX system calls) implemented in JavaScript
109
110[chat]: https://onp4.com/@vadim/~memfs
111[chat-badge]: https://img.shields.io/badge/Chat-%F0%9F%92%AC-green?style=flat&logo=chat&link=https://onp4.com/@vadim/~memfs
112[npm-url]: https://www.npmjs.com/package/memfs
113[npm-badge]: https://img.shields.io/npm/v/memfs.svg
114[travis-url]: https://travis-ci.org/streamich/memfs
115[travis-badge]: https://travis-ci.org/streamich/memfs.svg?branch=master
116[memfs]: https://github.com/streamich/memfs
117[unionfs]: https://github.com/streamich/unionfs
118[linkfs]: https://github.com/streamich/linkfs
119[spyfs]: https://github.com/streamich/spyfs
120[fs-monkey]: https://github.com/streamich/fs-monkey
121
122## License
123
124[Unlicense](./LICENSE) - public domain.