Node.js v14 to v16

AugustinMauroy

Node.js v14 to v16

This article cover a part of the migration from Node.js v14 to v16. The userland migrations team is working on more codemods to help you with the migration.

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

create-require-from-path

Node.js v16 replaced the createRequireFromPath function, deprecated in DEP0148, with the modern createRequire function. This codemod replaces calls of the deprecated function with the modern alternative mentioned.

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

Example:

import { createRequireFromPath } from 'node:module';

const requireFromPath = createRequireFromPath('/path/to/module');
const myModule = requireFromPath('./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:

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

process-mainModule-to-require-main

The process.mainModule property was deprecated (DEP0144) in favor of require.main. This codemod replaces calls of the deprecated property with the modern alternative mentioned.

npx codemod run @nodejs/process-mainModule-to-require-main

Example:

if (process.mainModule) {
  console.log('This script is the main module');
}

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:

// 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 });

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:

import { tmpDir } from 'node:os';

const foo = tmpDir();