{"id":16347659,"url":"https://github.com/privatenumber/fs-require","last_synced_at":"2025-03-21T00:30:22.153Z","repository":{"id":47627120,"uuid":"370157859","full_name":"privatenumber/fs-require","owner":"privatenumber","description":"Create a require() function from any file-system. Great for in-memory fs testing!","archived":false,"fork":false,"pushed_at":"2022-12-05T14:36:38.000Z","size":347,"stargazers_count":16,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2025-03-17T12:06:34.717Z","etag":null,"topics":["fs","fs-require","memfs","require","testing"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/privatenumber.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-23T21:09:31.000Z","updated_at":"2024-08-28T22:09:24.000Z","dependencies_parsed_at":"2023-01-24T05:30:53.577Z","dependency_job_id":null,"html_url":"https://github.com/privatenumber/fs-require","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/privatenumber%2Ffs-require","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/privatenumber%2Ffs-require/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/privatenumber%2Ffs-require/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/privatenumber%2Ffs-require/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/privatenumber","download_url":"https://codeload.github.com/privatenumber/fs-require/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244717078,"owners_count":20498279,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fs","fs-require","memfs","require","testing"],"created_at":"2024-10-11T00:44:34.734Z","updated_at":"2025-03-21T00:30:21.710Z","avatar_url":"https://github.com/privatenumber.png","language":"TypeScript","readme":"# fs-require [![Latest version](https://badgen.net/npm/v/fs-require)](https://npm.im/fs-require) [![Monthly downloads](https://badgen.net/npm/dm/fs-require)](https://npm.im/fs-require) [![Install size](https://packagephobia.now.sh/badge?p=fs-require)](https://packagephobia.now.sh/result?p=fs-require) [![Bundle size](https://badgen.net/bundlephobia/minzip/fs-require)](https://bundlephobia.com/result?p=fs-require)\n\nCreate a `require()` function from any file-system.\n\nPass in a [virtual file-system](https://github.com/streamich/memfs) for in-memory testing without writing to disk.\n\n### Features\n- 💞 Works well with [memfs](https://github.com/streamich/memfs)!\n- 🪄 Resolves implicit entry `index` and implicit extensions `js` and `json`\n- 🗺 Resolves relative and absolute paths\n- 📍 `__dirname` \u0026 `__filename`\n- ✅ `require.resolve()` \u0026 `require.cache`\n- 👻 Mocks `fs` within fsRequire\n- 👣 Call stack shows paths with `fs-require://` protocol\n\n\u003csub\u003eSupport this project by ⭐️ starring and sharing it. [Follow me](https://github.com/privatenumber) to see what other cool projects I'm working on! ❤️\u003c/sub\u003e\n\n## 🚀 Install\n\n```bash\nnpm i fs-require\n```\n\n## 🙋‍♀️ Why?\nUsing fs-require with [memfs](https://github.com/streamich/memfs) is a great combination for writing tests that interact with the file-system.\n\nTesting functionality that interacts with the file-system can be brittle because they expect a clean slate and can also be dangerous if the path is wrong. Creating a virtual file-system with `memfs` and testing its contents with `fsRequire` makes it secure and fast!\n\n\n## 👨‍🏫 Usage\n\n```js\nimport { Volume } from 'memfs'\nimport { createFsRequire } from 'fs-require'\n\n// Create a virtual fs from JSON\nconst virtualFs = Volume.fromJSON({\n    '/hello-world.js': `\n        module.exports = function () {\n            return 'Hello world!'\n        }\n    `\n})\n\n// Create fsRequire\nconst fsRequire = createFsRequire(virtualFs)\n\n// Import virtual module\nconst helloWorld = fsRequire('/hello-world')\n\nconsole.log(helloWorld()) // Hello world!\n```\n\n## ⚙️ API\n\n### createFsRequire(fs, options?)\nReturns a `fsRequire(modulePath)` function that resolves from the file-system passed in.\n\n#### fs\nType: `FileSystem`\n\nRequired\n\nThe file-system to resolve requires from.\n\n#### options\n##### options.fs\n\nType: `boolean | FileSystem`\n\nCode executed the virtual file-system may `require('fs')` and this may either pose as a security concern or yield inconsistent results as the virtual file won't not accessible through the actual `fs` module.\n\nBy default `require('fs')` is shimmed to the file-system passed into `createFsRequire`.\n\nTo disable this behavior and resolve to the real `fs` module, set this to `true`.\n\nYou can also pass in a different file-system too.\n\n\n### fsRequire(modulePath)\n\n#### modulePath\nType: `string`\n\nRequired\n\nPath to the module you want to \"require\". Mocks Node.js [`require`](https://nodejs.org/api/modules.html#requireid).\n\n### fsRequire.resolve(modulePath)\n\n#### modulePath\nType: `string`\n\nRequired\n\nPath to the module you want to \"resolve\". Mocks Node.js [`require.resolve`](https://nodejs.org/api/modules.html#requireresolverequest-options).\n\n### fsRequire.cache\n\nType: `Record\u003cstring, Module\u003e`\n\nAn object that contains the cache for modules that have been loaded so far. The key is the absolute path to the module, and the value is the module instance. Mocks Node.js [`require.cache`](https://nodejs.org/api/modules.html#requirecache).\n\nTo re-load a module that has already been loaded, you can delete the cache the same way you would in Node.js:\n\n```js\ndelete fsRequire.cache[fsRequire.resolve('/some-module.js')]\n```\n\n## 💁‍♂️ FAQ\n### Can it resolve case insensitive paths?\nCase sensitivity in paths is a file-system concern so it would depend on the `fs` passed in. For example, [macOS (native fs) is case insensitive](https://discussions.apple.com/thread/251191099#:~:text=No.,have%20two%20files%20named%20File.). [memfs is case sensitive](https://github.com/streamich/memfs/issues/533).\n\n\n## 👨‍👩‍👧 Related\n- [fs-monkey](https://github.com/streamich/fs-monkey) - By the same author of [memfs](https://github.com/streamich/memfs). Patches the global `require` to access a virtual fs.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprivatenumber%2Ffs-require","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprivatenumber%2Ffs-require","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprivatenumber%2Ffs-require/lists"}