Перейти до вмісту

Node.js v16 to v18

AugustinMauroy

Node.js v16 to v18

!

This article covers a part of the migration from Node.js v16 to v18. 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 v16 to v18.

dns-lookup-options-coercion

In Node.js v18, the DEP0153 deprecation reached end-of-life, which means that the dns.lookup() and dnsPromises.lookup() functions will no longer coerce option values to their proper types. This codemod will help you convert any literal option values to their proper types.

You can find this codemod in the Codemod Registry.

Examples:

  const dns = require("node:dns");

- dns.lookup("example.com", { family: "4", all: 1 }, callback);
+ dns.lookup("example.com", { family: 4, all: true }, callback);
  import { lookup } from "node:dns/promises";

- await lookup("example.com", { family: "6", verbatim: 0 });
+ await lookup("example.com", { family: 6, verbatim: false });

err-invalid-callback

In Node.js v18, the DEP0159 deprecation was introduced, which deprecated the ERR_INVALID_CALLBACK error code in favor of ERR_INVALID_ARG_TYPE. This codemod will help you replace any references to the old error code with the new one.

The source code for this codemod can be found in the err-invalid-callback directory.

You can find this codemod in the Codemod Registry.

Examples:

  try {
    fs.readFile("file.txt", "invalid-callback");
  } catch (err) {
-   if (err.code === "ERR_INVALID_CALLBACK") {
+   if (err.code === "ERR_INVALID_ARG_TYPE") {
      console.error("Invalid callback provided");
    }
  }

Also handles deduplication when both codes were already checked:

  const isCallbackError =
-   err.code === "ERR_INVALID_CALLBACK" ||
-   err.code === "ERR_INVALID_ARG_TYPE";
+   err.code === "ERR_INVALID_ARG_TYPE";