Git Product home page Git Product logo

fs-require's Introduction

fs-require Latest version Monthly downloads Install size Bundle size

Create a require() function from any file-system.

Pass in a virtual file-system for in-memory testing without writing to disk.

Features

  • ๐Ÿ’ž Works well with memfs!
  • ๐Ÿช„ Resolves implicit entry index and implicit extensions js and json
  • ๐Ÿ—บ Resolves relative and absolute paths
  • ๐Ÿ“ __dirname & __filename
  • โœ… require.resolve() & require.cache
  • ๐Ÿ‘ป Mocks fs within fsRequire
  • ๐Ÿ‘ฃ Call stack shows paths with fs-require:// protocol

Support this project by โญ๏ธ starring and sharing it. Follow me to see what other cool projects I'm working on! โค๏ธ

๐Ÿš€ Install

npm i fs-require

๐Ÿ™‹โ€โ™€๏ธ Why?

Using 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!

๐Ÿ‘จโ€๐Ÿซ Usage

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!

โš™๏ธ API

createFsRequire(fs, options?)

Returns a fsRequire(modulePath) function that resolves from the file-system passed in.

fs

Type: FileSystem

Required

The file-system to resolve requires from.

options

options.fs

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.

fsRequire(modulePath)

modulePath

Type: string

Required

Path to the module you want to "require". Mocks Node.js require.

fsRequire.resolve(modulePath)

modulePath

Type: string

Required

Path to the module you want to "resolve". Mocks Node.js require.resolve.

fsRequire.cache

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')]

๐Ÿ’โ€โ™‚๏ธ FAQ

Can it resolve case insensitive paths?

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.

๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ง Related

  • fs-monkey - By the same author of memfs. Patches the global require to access a virtual fs.

fs-require's People

Contributors

privatenumber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

withkonvsuu

fs-require's Issues

feat: add support for require.cache

Is your feature request related to a problem? Please describe.

In Node, you can do delete require.cache[require.resolve(module)] to reload a module.

Describe the solution you'd like

Expose cache object on require.cache.

Describe alternatives you've considered

Additional context

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.