Node.js v14 to v16

This page provides a list of codemods to help you migrate your code from Node.js v14 to v16.

create-require-from-path

In Node.js v16, the createRequire function was introduced to allow you to create a require function that can be used in ESM modules. This codemod will help you replace the old createRequireFromPath function with the new createRequire function.

So this codemod handle DEP0130.

npx codemod run @nodejs/create-require-from-path

Example:

Before:

import { createRequireFromPath } from 'node:module';

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

After:

import { createRequire } from 'node:module';

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

process-main-module

The process.mainModule property was deprecated in favor of require.main. This codemod will help you replace the old process.mainModule usage with the new require.main usage.

So the codemod handle DEP0138.

npx codemod run @nodejs/process-main-module

Example:

Before:

if (process.mainModule === 'mod.js') {
  // cli thing
} else {
  // module thing
}

After:

if (require.main === 'mod.js') {
  // cli thing
} else {
  // module thing
}

rmdir

The fs.rmdir function was deprecated in favor of fs.rm with the { recursive: true } option. This codemod will help you replace the old fs.rmdir function with the new fs.rm function.

so this codemod handle DEP0147.

npx codemod run @nodejs/rmdir

Example:

Before:

// Using fs.rmdir with the recursive option
fs.rmdir(path, { recursive: true }, callback);

// Using fs.rmdirSync with the recursive option
fs.rmdirSync(path, { recursive: true });

// Using fs.promises.rmdir with the recursive option
fs.promises.rmdir(path, { recursive: true });

After:

// Using fs.rm with recursive and force options
fs.rm(path, { recursive: true, force: true }, callback);

// Using fs.rmSync with recursive and force options
fs.rmSync(path, { recursive: true, force: true });

// Using fs.promises.rm with recursive and force options
fs.promises.rm(path, { recursive: true, force: true });

tmpDir-to-tmpdir

The tmpDir function was renamed to tmpdir in Node.js v16. This codemod will help you replace all instances of tmpDir with tmpdir.

So the codemod handles DEP0022.

npx codemod run @nodejs/tmpDir-to-tmpdir

Example:

Before:

import { tmpDir } from 'node:os';

const foo = tmpDir();

After:

import { tmpdir } from 'node:os';

const foo = tmpdir();