Nodejs 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@latest @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');

rmdir

In Node.js v16, 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@latest @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 });
Durată de citire
1 min.
Autor
Contribuie
Editează această pagină
Cuprins
  1. create-require-from-path
  2. rmdir