Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fs-require

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fs-require

Create a require() function from any file-system

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.5K
decreased by-16.29%
Maintainers
1
Weekly downloads
 
Created
Source

fs-require Latest version Install size Bundle size

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

Features

  • 💞 Works well with memfs!
  • 🪄 Resolves implicit entry index and implicit extensions js and json
  • 🗺 Resolves relative and absolute paths
  • 📍 __dirname & __filename
  • 👻 Mocks fs within fsRequire
  • 👣 Call stack shows paths with fs-require:// protocol

🙋‍♀️ 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 require(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.

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

Keywords

FAQs

Package last updated on 20 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc