Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
create-require
Advanced tools
The create-require npm package is designed to dynamically create a `require` function at a given path. This is particularly useful in scenarios where you need to resolve modules or require files relative to a specific directory, rather than the current working directory of the process. It can be used to simulate the behavior of Node.js's `require` in different contexts or to dynamically load modules from various locations in a filesystem.
Creating a custom require function for a specific directory
This feature allows you to create a new `require` function that resolves modules as if it was called from the specified directory. This is useful for loading modules from a directory different from the current script's directory.
const createRequire = require('create-require');
const myRequire = createRequire('/path/to/my/directory');
const myModule = myRequire('./myModule');
Similar to create-require, import-fresh allows you to require a module afresh, ensuring that it is re-evaluated. It's useful for testing and situations where you need to bypass Node's require cache. However, it doesn't allow specifying a directory for module resolution like create-require does.
Proxyquire is designed for overriding dependencies during testing. It allows you to create a custom `require` function that can return mock modules instead of the real ones. While it serves a different primary purpose (mocking for tests), it shares the concept of creating a custom require function with create-require.
create-require
Polyfill for Node.js (<= v12.2.0) module.createRequire
yarn add create-require
npm install create-require
function createRequire (filename: string | URL): NodeRequire;
const createRequire = require('create-require')
const require = createRequire('path/to/test.js')
const module = require('./test-sibling-module')
FAQs
Polyfill for Node.js module.createRequire (<= v12.2.0)
The npm package create-require receives a total of 11,715,185 weekly downloads. As such, create-require popularity was classified as popular.
We found that create-require demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.