package
Create a require() function from any file-system
npm install fs-requireGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
16.1 kB
Includes dependencies.
Create a require() function from any file-system.
Pass in a virtual file-system for in-memory testing without writing to disk.
index and implicit extensions js and json__dirname & __filenamerequire.resolve() & require.cachefs within fsRequirefs-require:// protocolSupport this project by ⭐️ starring and sharing it. Follow me to see what other cool projects I'm working on! ❤️
npm i fs-requireUsing fs-require with memfs is a great combination for writing tests that interact with the file-system.
Testing 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!
import { Volume } from 'memfs'
import { createFsRequire } from 'fs-require'
// Create a virtual fs from JSON
const virtualFs = Volume.fromJSON({
'/hello-world.js': `
module.exports = function () {
return 'Hello world!'
}
`
})
// Create fsRequire
const fsRequire = createFsRequire(virtualFs)
// Import virtual module
const helloWorld = fsRequire('/hello-world')
console.log(helloWorld()) // Hello world!Returns a fsRequire(modulePath) function that resolves from the file-system passed in.
Type: FileSystem
Required
The file-system to resolve requires from.
Type: boolean | FileSystem
Code 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.
By default require('fs') is shimmed to the file-system passed into createFsRequire.
To disable this behavior and resolve to the real fs module, set this to true.
You can also pass in a different file-system too.
Type: string
Required
Path to the module you want to "require". Mocks Node.js require.
Relative paths and bare specifiers (package names) resolve from the file that called createFsRequire(), like require resolves from the file it's used in. If the caller isn't a file (e.g. node -e or the REPL), relative paths are passed to the file-system as relative paths, and bare specifiers resolve from fs-require's own location.
Type: string
Required
Path to the module you want to "resolve". Mocks Node.js require.resolve for paths that start with / or . or are absolute on the current platform: it returns the matching path in the file-system, and throws a MODULE_NOT_FOUND error when nothing matches. It tries the path as a file, then with .js and .json appended, before looking for a directory. For a directory, it reads package.json#main if present and tries that path as a file or a directory index. If it does not match, it tries the directory's index.js and index.json.
Any other request, such as a package name, is returned unchanged without checking that it is installed, because fsRequire() doesn't read it from the file-system: it passes it to Node.js, except for the fs requests that the fs option shims.
Type: Record<string, Module>
An 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.
To re-load a module that has already been loaded, you can delete the cache the same way you would in Node.js:
delete fsRequire.cache[fsRequire.resolve('/some-module.js')]Case 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. memfs is case sensitive.