Skip to content

Commit

Permalink
docs: add codemods section for migrating to Express 5
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohansebas committed Jan 27, 2025
1 parent 899dbfd commit eb635af
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions en/guide/migrating-5.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ npm install "express@^{{ site.data.express.next_version }}"

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.

## Express 5 Codemods

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.

Run the following command for run all the codemods available:

```sh
npx @expressjs/codemod upgrade
```

If you want to run a specific codemod, you can run the following command:

```sh
npx @expressjs/codemod name-of-the-codemod
```

You can find the list of available codemods [here](https://github.com/expressjs/codemod?tab=readme-ov-file#available-codemods).

<h2 id="changes">Changes in Express 5</h2>

**Removed methods and properties**
Expand Down Expand Up @@ -72,6 +90,16 @@ Express 5 no longer supports the `app.del()` function. If you use this function,

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.

{% capture codemod-deprecated-signatures %}
You can replace the deprecated signatures with the following command:

```sh
npx @expressjs/codemod v4-deprecated-signatures
```
{% endcapture %}

{% include admonitions/note.html content=codemod-deprecated-signatures %}

```js
// v4
app.del('/user/:id', (req, res) => {
Expand Down Expand Up @@ -152,6 +180,8 @@ app.post('/user', (req, res) => {

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)`.

{% include admonitions/note.html content=codemod-deprecated-signatures %}

```js
// v4
app.post('/user', (req, res) => {
Expand All @@ -168,6 +198,8 @@ app.post('/user', (req, res) => {

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)`.

{% include admonitions/note.html content=codemod-deprecated-signatures %}

```js
// v4
app.post('/user', (req, res) => {
Expand All @@ -184,6 +216,8 @@ app.post('/user', (req, res) => {

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

{% include admonitions/note.html content=codemod-deprecated-signatures %}

```js
// v4
app.get('/user', (req, res) => {
Expand Down Expand Up @@ -217,6 +251,8 @@ app.get('/user', (req, res) => {

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)`.

{% include admonitions/note.html content=codemod-deprecated-signatures %}

```js
// v4
app.get('/user', (req, res) => {
Expand All @@ -234,6 +270,8 @@ app.get('/user', (req, res) => {
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.
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.

{% include admonitions/note.html content=codemod-deprecated-signatures %}

```js
// v4
app.get('/user', (req, res) => {
Expand All @@ -250,6 +288,8 @@ app.get('/user', (req, res) => {

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

{% include admonitions/note.html content=codemod-deprecated-signatures %}

```js
// v4
app.get('/user', (req, res) => {
Expand Down

0 comments on commit eb635af

Please sign in to comment.