|
| 1 | +--- |
| 2 | +date: '2025-10-28T00:01:00.000Z' |
| 3 | +category: migrations |
| 4 | +title: Node.js v14 to v16 |
| 5 | +layout: blog-post |
| 6 | +author: AugustinMauroy |
| 7 | +--- |
| 8 | + |
| 9 | +# Node.js v14 to v16 |
| 10 | + |
| 11 | +<AlertBox level="info" title="!"> |
| 12 | + This article covers a part of the migration from Node.js v14 to v16. The |
| 13 | + userland migrations team is working on more codemods to help you with the |
| 14 | + migration. |
| 15 | +</AlertBox> |
| 16 | + |
| 17 | +This page provides a list of codemods to help you migrate your code from Node.js v14 to v16. |
| 18 | + |
| 19 | +## `create-require-from-path` |
| 20 | + |
| 21 | +Node.js v16 replaced the [`createRequireFromPath`](https://nodejs.org/api/module.html#module_module_createrequirefrompath) function, deprecated in [DEP0148](https://nodejs.org/api/deprecations.html#DEP0148), with the modern [`createRequire`](https://nodejs.org/api/module.html#module_module_createrequire) function. This codemod replaces calls of the deprecated function with the modern alternative mentioned. |
| 22 | + |
| 23 | +The source code for this codemod can be found in the [create-require-from-path directory](https://github.com/nodejs/userland-migrations/tree/main/recipes/create-require-from-path). |
| 24 | + |
| 25 | +You can find this codemod in the [Codemod Registry](https://app.codemod.com/registry/@nodejs/create-require-from-path). |
| 26 | + |
| 27 | +```bash |
| 28 | +npx codemod run @nodejs/create-require-from-path |
| 29 | +``` |
| 30 | + |
| 31 | +### Example: |
| 32 | + |
| 33 | +```js displayName="Before" |
| 34 | +import { createRequireFromPath } from 'node:module'; |
| 35 | + |
| 36 | +const requireFromPath = createRequireFromPath('/path/to/module'); |
| 37 | +const myModule = requireFromPath('./myModule.cjs'); |
| 38 | +``` |
| 39 | + |
| 40 | +```js displayName="After" |
| 41 | +import { createRequire } from 'node:module'; |
| 42 | + |
| 43 | +const require = createRequire('/path/to/module'); |
| 44 | +const myModule = require('./myModule.cjs'); |
| 45 | +``` |
| 46 | + |
| 47 | +## `process-main-module` |
| 48 | + |
| 49 | +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. |
| 50 | + |
| 51 | +So the codemod handle [DEP0138](https://nodejs.org/api/deprecations.html#DEP0138). |
| 52 | + |
| 53 | +The source code for this codemod can be found in the [process-main-module directory](https://github.com/nodejs/userland-migrations/tree/main/recipes/process-main-module). |
| 54 | + |
| 55 | +You can find this codemod in the [Codemod Registry](https://app.codemod.com/registry/@nodejs/process-main-module). |
| 56 | + |
| 57 | +```bash |
| 58 | +npx codemod run @nodejs/process-main-module |
| 59 | +``` |
| 60 | + |
| 61 | +### Example: |
| 62 | + |
| 63 | +```js displayName="Before" |
| 64 | +if (process.mainModule === 'mod.js') { |
| 65 | + // cli thing |
| 66 | +} else { |
| 67 | + // module thing |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +```js displayName="After" |
| 72 | +if (require.main === 'mod.js') { |
| 73 | + // cli thing |
| 74 | +} else { |
| 75 | + // module thing |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## `process-mainModule-to-require-main` |
| 80 | + |
| 81 | +The [`process.mainModule`](https://nodejs.org/api/process.html#process_process_mainmodule) property was deprecated ([DEP0144](https://nodejs.org/api/deprecations.html#DEP0144)) in favor of [`require.main`](https://nodejs.org/api/modules.html#modules_accessing_the_main_module). This codemod replaces calls of the deprecated property with the modern alternative mentioned. |
| 82 | + |
| 83 | +The source code for this codemod can be found in the [process-mainModule-to-require-main directory](https://github.com/nodejs/userland-migrations/tree/main/recipes/process-main-module). |
| 84 | + |
| 85 | +You can find this codemod in the [Codemod Registry](https://app.codemod.com/registry/@nodejs/process-mainModule-to-require-main). |
| 86 | + |
| 87 | +```bash |
| 88 | +npx codemod run @nodejs/process-mainModule-to-require-main |
| 89 | +``` |
| 90 | + |
| 91 | +### Example: |
| 92 | + |
| 93 | +```js displayName="Before" |
| 94 | +if (process.mainModule) { |
| 95 | + console.log('This script is the main module'); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +```js displayName="After" |
| 100 | +if (require.main === module) { |
| 101 | + console.log('This script is the main module'); |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## `rmdir` |
| 106 | + |
| 107 | +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. |
| 108 | + |
| 109 | +so this codemod handle [DEP0147](https://nodejs.org/api/deprecations.html#DEP0147). |
| 110 | + |
| 111 | +The source code for this codemod can be found in the [rmdir directory](https://github.com/nodejs/userland-migrations/tree/main/recipes/rmdir). |
| 112 | + |
| 113 | +You can find this codemod in the [Codemod Registry](https://app.codemod.com/registry/@nodejs/rmdir). |
| 114 | + |
| 115 | +```bash |
| 116 | +npx codemod run @nodejs/rmdir |
| 117 | +``` |
| 118 | + |
| 119 | +### Example: |
| 120 | + |
| 121 | +```js displayName="Before" |
| 122 | +// Using fs.rmdir with the recursive option |
| 123 | +fs.rmdir(path, { recursive: true }, callback); |
| 124 | + |
| 125 | +// Using fs.rmdirSync with the recursive option |
| 126 | +fs.rmdirSync(path, { recursive: true }); |
| 127 | + |
| 128 | +// Using fs.promises.rmdir with the recursive option |
| 129 | +fs.promises.rmdir(path, { recursive: true }); |
| 130 | +``` |
| 131 | + |
| 132 | +```js displayName="After" |
| 133 | +// Using fs.rm with recursive and force options |
| 134 | +fs.rm(path, { recursive: true, force: true }, callback); |
| 135 | + |
| 136 | +// Using fs.rmSync with recursive and force options |
| 137 | +fs.rmSync(path, { recursive: true, force: true }); |
| 138 | + |
| 139 | +// Using fs.promises.rm with recursive and force options |
| 140 | +fs.promises.rm(path, { recursive: true, force: true }); |
| 141 | +``` |
| 142 | + |
| 143 | +## `tmpDir-to-tmpdir` |
| 144 | + |
| 145 | +The `tmpDir` function was renamed to `tmpdir` in Node.js v16. This codemod will help you replace all instances of `tmpDir` with `tmpdir`. |
| 146 | + |
| 147 | +So the codemod handles [DEP0022](https://nodejs.org/docs/latest/api/deprecations.html#dep0022-ostmpdir). |
| 148 | + |
| 149 | +The source code for this codemod can be found in the [tmpdir-to-tmpdir directory](https://github.com/nodejs/userland-migrations/tree/main/recipes/tmpdir-to-tmpdir). |
| 150 | + |
| 151 | +You can find this codemod in the [Codemod Registry](https://app.codemod.com/registry/@nodejs/tmpDir-to-tmpdir). |
| 152 | + |
| 153 | +```bash |
| 154 | +npx codemod run @nodejs/tmpDir-to-tmpdir |
| 155 | +``` |
| 156 | + |
| 157 | +### Example: |
| 158 | + |
| 159 | +```js displayName="Before" |
| 160 | +import { tmpDir } from 'node:os'; |
| 161 | + |
| 162 | +const foo = tmpDir(); |
| 163 | +``` |
| 164 | + |
| 165 | +```js displayName="After" |
| 166 | +import { tmpdir } from 'node:os'; |
| 167 | + |
| 168 | +const foo = tmpdir(); |
| 169 | +``` |
0 commit comments