npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fs-require

v1.6.0

Published

Create a require() function from any file-system

Downloads

46,565

Readme

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.