Skip to main content
This recipe transforms the usage of the deprecated createRequireFromPath function to use the modern createRequire function from the node:module module.

What It Does

The codemod transforms:
  • createRequireFromPath import/require → createRequire
  • createRequireFromPath(path) calls → createRequire(path)
  • Variable names from requireFromPathrequire (where appropriate)

Usage

Example

Before
After

Why Migrate?

createRequireFromPath was deprecated in favor of createRequire, which:
  • Has a clearer, more concise name
  • Provides the same functionality with a better API
  • Aligns with Node.js module system conventions
  • Is the recommended approach going forward
The createRequire function is commonly used in ES modules to load CommonJS modules or to use require.resolve() for path resolution.

Common Use Cases

Loading CommonJS from ESM

Resolving Module Paths

Deprecation Reference

This migration addresses DEP0130.
createRequireFromPath was deprecated in Node.js v12.2.0 and may be removed in future versions. Migrate your code to avoid potential breaking changes.