Skip to content

Commit 22f4946

Browse files
authored
docs: add codemods section for migrating to Express 5 (#1739)
1 parent 502d079 commit 22f4946

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

en/guide/migrating-5.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ npm install "express@^{{ site.data.express.next_version }}"
2121

2222
You can then run your automated tests to see what fails, and fix problems according to the updates listed below. After addressing test failures, run your app to see what errors occur. You'll find out right away if the app uses any methods or properties that are not supported.
2323

24+
## Express 5 Codemods
25+
26+
To help you migrate your express server, we have created a set of codemods that will help you automatically update your code to the latest version of Express.
27+
28+
Run the following command for run all the codemods available:
29+
30+
```sh
31+
npx @expressjs/codemod upgrade
32+
```
33+
34+
If you want to run a specific codemod, you can run the following command:
35+
36+
```sh
37+
npx @expressjs/codemod name-of-the-codemod
38+
```
39+
40+
You can find the list of available codemods [here](https://github.com/expressjs/codemod?tab=readme-ov-file#available-codemods).
41+
2442
<h2 id="changes">Changes in Express 5</h2>
2543

2644
**Removed methods and properties**
@@ -74,6 +92,16 @@ Express 5 no longer supports the `app.del()` function. If you use this function,
7492

7593
Initially, `del` was used instead of `delete`, because `delete` is a reserved keyword in JavaScript. However, as of ECMAScript 6, `delete` and other reserved keywords can legally be used as property names.
7694

95+
{% capture codemod-deprecated-signatures %}
96+
You can replace the deprecated signatures with the following command:
97+
98+
```plain-text
99+
npx @expressjs/codemod v4-deprecated-signatures
100+
```
101+
{% endcapture %}
102+
103+
{% include admonitions/note.html content=codemod-deprecated-signatures %}
104+
77105
```js
78106
// v4
79107
app.del('/user/:id', (req, res) => {
@@ -100,6 +128,16 @@ The following method names have been pluralized. In Express 4, using the old met
100128

101129
`req.acceptsLanguage()` is replaced by `req.acceptsLanguages()`.
102130

131+
{% capture codemod-pluralized-methods %}
132+
You can replace the deprecated signatures with the following command:
133+
134+
```plain-text
135+
npx @expressjs/codemod pluralized-methods
136+
```
137+
{% endcapture %}
138+
139+
{% include admonitions/note.html content=codemod-pluralized-methods %}
140+
103141
```js
104142
// v4
105143
app.all('/', (req, res) => {
@@ -130,6 +168,16 @@ This should not affect your code if you follow the Express 4 documentation of [a
130168

131169
This potentially confusing and dangerous method of retrieving form data has been removed. You will now need to specifically look for the submitted parameter name in the `req.params`, `req.body`, or `req.query` object.
132170

171+
{% capture codemod-req-param %}
172+
You can replace the deprecated signatures with the following command:
173+
174+
```plain-text
175+
npx @expressjs/codemod req-param
176+
```
177+
{% endcapture %}
178+
179+
{% include admonitions/note.html content=codemod-req-param %}
180+
133181
```js
134182
// v4
135183
app.post('/user', (req, res) => {
@@ -154,6 +202,8 @@ app.post('/user', (req, res) => {
154202

155203
Express 5 no longer supports the signature `res.json(obj, status)`. Instead, set the status and then chain it to the `res.json()` method like this: `res.status(status).json(obj)`.
156204

205+
{% include admonitions/note.html content=codemod-deprecated-signatures %}
206+
157207
```js
158208
// v4
159209
app.post('/user', (req, res) => {
@@ -170,6 +220,8 @@ app.post('/user', (req, res) => {
170220

171221
Express 5 no longer supports the signature `res.jsonp(obj, status)`. Instead, set the status and then chain it to the `res.jsonp()` method like this: `res.status(status).jsonp(obj)`.
172222

223+
{% include admonitions/note.html content=codemod-deprecated-signatures %}
224+
173225
```js
174226
// v4
175227
app.post('/user', (req, res) => {
@@ -186,6 +238,8 @@ app.post('/user', (req, res) => {
186238

187239
Express 5 no longer supports the signature `res.redirect(url, status)`. Instead, use the following signature: `res.redirect(status, url)`.
188240

241+
{% include admonitions/note.html content=codemod-deprecated-signatures %}
242+
189243
```js
190244
// v4
191245
app.get('/user', (req, res) => {
@@ -203,6 +257,16 @@ app.get('/user', (req, res) => {
203257

204258
Express 5 no longer supports the magic string `back` in the `res.redirect()` and `res.location()` methods. Instead, use the `req.get('Referrer') || '/'` value to redirect back to the previous page. In Express 4, the res.`redirect('back')` and `res.location('back')` methods were deprecated.
205259

260+
{% capture codemod-magic-redirect %}
261+
You can replace the deprecated signatures with the following command:
262+
263+
```plain-text
264+
npx @expressjs/codemod magic-redirect
265+
```
266+
{% endcapture %}
267+
268+
{% include admonitions/note.html content=codemod-magic-redirect %}
269+
206270
```js
207271
// v4
208272
app.get('/user', (req, res) => {
@@ -219,6 +283,8 @@ app.get('/user', (req, res) => {
219283

220284
Express 5 no longer supports the signature `res.send(obj, status)`. Instead, set the status and then chain it to the `res.send()` method like this: `res.status(status).send(obj)`.
221285

286+
{% include admonitions/note.html content=codemod-deprecated-signatures %}
287+
222288
```js
223289
// v4
224290
app.get('/user', (req, res) => {
@@ -236,6 +302,8 @@ app.get('/user', (req, res) => {
236302
Express 5 no longer supports the signature `res.send(status)`, where `status` is a number. Instead, use the `res.sendStatus(statusCode)` function, which sets the HTTP response header status code and sends the text version of the code: "Not Found", "Internal Server Error", and so on.
237303
If you need to send a number by using the `res.send()` function, quote the number to convert it to a string, so that Express does not interpret it as an attempt to use the unsupported old signature.
238304

305+
{% include admonitions/note.html content=codemod-deprecated-signatures %}
306+
239307
```js
240308
// v4
241309
app.get('/user', (req, res) => {
@@ -252,6 +320,8 @@ app.get('/user', (req, res) => {
252320

253321
The `res.sendfile()` function has been replaced by a camel-cased version `res.sendFile()` in Express 5.
254322

323+
{% include admonitions/note.html content=codemod-deprecated-signatures %}
324+
255325
```js
256326
// v4
257327
app.get('/user', (req, res) => {

0 commit comments

Comments
 (0)