> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/nodejs/userland-migrations/llms.txt
> Use this file to discover all available pages before exploring further.

# createRequireFromPath Migration (DEP0130)

> Migrate from deprecated createRequireFromPath to createRequire for creating require functions in ES modules

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 `requireFromPath` → `require` (where appropriate)

## Usage

```bash theme={null}
npx codemod nodejs/create-require-from-path
```

## Example

<Tabs>
  <Tab title="CommonJS">
    ```javascript Before theme={null}
    const { createRequireFromPath } = require('node:module');

    // Using createRequireFromPath
    const requireFromPath = createRequireFromPath('/path/to/module');
    const myModule = requireFromPath('./myModule.cjs');
    ```

    ```javascript After theme={null}
    const { createRequire } = require('node:module');

    // Using createRequire with a specific path
    const require = createRequire('/path/to/module');
    const myModule = require('./myModule.cjs');
    ```
  </Tab>

  <Tab title="ESM">
    ```javascript Before theme={null}
    import { createRequireFromPath } from 'node:module';

    const requireFromPath = createRequireFromPath('/path/to/module');
    const myModule = requireFromPath('./myModule.cjs');
    ```

    ```javascript After theme={null}
    import { createRequire } from 'node:module';

    const require = createRequire('/path/to/module');
    const myModule = require('./myModule.cjs');
    ```
  </Tab>
</Tabs>

## 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

<Tip>
  The `createRequire` function is commonly used in ES modules to load CommonJS modules or to use `require.resolve()` for path resolution.
</Tip>

## Common Use Cases

### Loading CommonJS from ESM

```javascript theme={null}
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';

const require = createRequire(import.meta.url);
const packageJson = require('./package.json');
```

### Resolving Module Paths

```javascript theme={null}
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const modulePath = require.resolve('some-module');
```

## Deprecation Reference

This migration addresses [DEP0130](https://nodejs.org/api/deprecations.html#DEP0130).

<Warning>
  `createRequireFromPath` was deprecated in Node.js v12.2.0 and may be removed in future versions. Migrate your code to avoid potential breaking changes.
</Warning>
