You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-functions/functions-node-upgrade-v4.md
+29-29Lines changed: 29 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,30 @@
1
1
---
2
-
title: Upgrade to Azure Functions Node.js programming model v4.x
3
-
description: This article shows you how to upgrade your existing function apps running on version 3.x of the Node.js programming model to version 4.x.
2
+
title: Upgrade to Azure Functions Node.js programming model v4
3
+
description: This article shows you how to upgrade your existing function apps running on v3 of the Node.js programming model to v4.
4
4
ms.service: azure-functions
5
5
ms.date: 03/15/2023
6
6
ms.devlang: javascript, typescript
7
7
ms.topic: how-to
8
8
---
9
9
10
-
# Upgrade to Azure Functions Node.js programming model v4.x
10
+
# Upgrade to Azure Functions Node.js programming model v4
11
11
12
-
The Node.js programming model for Azure Functions defines how you author serverless code in JavaScript or TypeScript. The version of the programming model matches the version of the [`@azure/functions`](https://www.npmjs.com/package/@azure/functions) npm package that should be included with your app. This article discusses the differences between both versions and how to upgrade an existing v3.x app. If you want to create a brand new v4.x app, see the tutorial for either [VS Code](./create-first-function-cli-node.md) or [Azure Functions Core Tools](./create-first-function-vs-code-node.md).
12
+
The Node.js programming model for Azure Functions defines how you author serverless code in JavaScript or TypeScript. The version of the programming model matches the version of the [`@azure/functions`](https://www.npmjs.com/package/@azure/functions) npm package that should be included with your app. This article discusses the differences between both versions and how to upgrade an existing v3 app. If you want to create a brand new v4 app, see the tutorial for either [VS Code](./create-first-function-cli-node.md) or [Azure Functions Core Tools](./create-first-function-vs-code-node.md).
13
13
14
14
> [!NOTE]
15
-
> v4.x of the Node.js programming model is currently in public preview. This version number is _not_ the same thing as the Azure Functions [runtime version](./functions-versions.md), which is coincidentally using "4" as its latest major version. Lastly, you can't mix v3.x and v4.x of the programming models.
15
+
> v4 of the Node.js programming model is currently in public preview. This version number is _not_ the same thing as the Azure Functions [runtime version](./functions-versions.md), which is coincidentally using "4" as its latest major version. Lastly, you can't mix v3 and v4 of the programming models.
16
16
17
17
## Motivation
18
18
19
-
v4.x was designed with the following goals in mind:
19
+
v4 was designed with the following goals in mind:
20
20
21
21
- Provide a familiar and intuitive experience to Node.js developers
22
22
- Make the file structure flexible with support for full customization
23
23
- Switch to a code-centric approach for defining function configuration
24
24
25
25
## Prerequisites
26
26
27
-
Before you start upgrading your app, v4.x of the Node.js programming model is only supported with the following minimum versions:
27
+
Before you start upgrading your app, v4 of the Node.js programming model is only supported with the following minimum versions:
@@ -34,17 +34,17 @@ Before you start upgrading your app, v4.x of the Node.js programming model is on
34
34
35
35
## npm package
36
36
37
-
For the first time, the [`@azure/functions`](https://www.npmjs.com/package/@azure/functions) npm package contains the primary source code that backs the Node.js programming model for Azure Functions. Previously, that code shipped directly in Azure and the npm package only had the TypeScript types. Moving forward both JavaScript and TypeScript users need to include this package in their app. v3.x apps _can_ include the npm package, but it isn't required.
37
+
For the first time, the [`@azure/functions`](https://www.npmjs.com/package/@azure/functions) npm package contains the primary source code that backs the Node.js programming model for Azure Functions. Previously, that code shipped directly in Azure and the npm package only had the TypeScript types. Moving forward both JavaScript and TypeScript users need to include this package in their app. v3 apps _can_ include the npm package, but it isn't required.
38
38
39
39
> [!TIP]
40
-
> _**Action Item**_: Make sure the `@azure/functions` package is listed in the `dependencies` section (not `devDependencies`) of your `package.json` file. You can install v4.x with the command
40
+
> _**Action Item**_: Make sure the `@azure/functions` package is listed in the `dependencies` section (not `devDependencies`) of your `package.json` file. You can install v4 with the command
41
41
> ```
42
42
> npm install @azure/functions@preview
43
43
> ```
44
44
45
45
## App entry point
46
46
47
-
v4.x of the programming model lets you structure your code however you want. The only files you need at the root of your app are `host.json` and `package.json`. Otherwise, you define the file structure by setting the `main` field in your `package.json` file. The `main` field can be set to a single file or multiple files by using a [glob pattern](https://wikipedia.org/wiki/Glob_(programming)). Common values for the `main` field may be:
47
+
v4 of the programming model lets you structure your code however you want. The only files you need at the root of your app are `host.json` and `package.json`. Otherwise, you define the file structure by setting the `main` field in your `package.json` file. The `main` field can be set to a single file or multiple files by using a [glob pattern](https://wikipedia.org/wiki/Glob_(programming)). Common values for the `main` field may be:
48
48
- TypeScript
49
49
- `dist/src/index.js`
50
50
- `dist/src/functions/*.js`
@@ -57,7 +57,7 @@ v4.x of the programming model lets you structure your code however you want. The
57
57
58
58
## Order of arguments
59
59
60
-
The trigger input is now the first argument to your function handler instead of the invocation context. The invocation context, now the second argument, was simplified in v4.x and isn't as required as the trigger input - it can be left off if you aren't using it.
60
+
The trigger input is now the first argument to your function handler instead of the invocation context. The invocation context, now the second argument, was simplified in v4 and isn't as required as the trigger input - it can be left off if you aren't using it.
61
61
62
62
> [!TIP]
63
63
> _**Action Item**_: Switch the order of your arguments. For example if you are using an http trigger, switch `(context, request)` to either `(request, context)` or just `(request)` if you aren't using the context.
@@ -67,9 +67,9 @@ The trigger input is now the first argument to your function handler instead of
67
67
Say goodbye 👋 to `function.json` files! All of the configuration that you were previously specifying in a `function.json` file is now defined directly in your TypeScript or JavaScript files. In addition, many properties now have a default so that you don't have to specify them every time.
68
68
69
69
> [!WARNING]
70
-
> You can't mix the v3.x and v4.x programming models. As soon as you register one v4.x function in your app, any v3.x functions registered in `function.json` files will be ignored.
70
+
> You can't mix the v3 and v4 programming models. As soon as you register one v4 function in your app, any v3 functions registered in `function.json` files will be ignored.
71
71
72
-
# [v4.x](#tab/v4)
72
+
# [v4](#tab/v4)
73
73
74
74
```javascript
75
75
const { app } = require("@azure/functions");
@@ -90,7 +90,7 @@ app.http('helloWorld1', {
90
90
91
91
No `function.json` file! ✨
92
92
93
-
# [v3.x](#tab/v3)
93
+
# [v3](#tab/v3)
94
94
95
95
```javascript
96
96
module.exports=asyncfunction (context, req) {
@@ -142,18 +142,18 @@ The `context` object has been simplified to reduce duplication and make it easie
142
142
143
143
The primary input is also called the "trigger" and is the only required input or output. You must have one and only one trigger.
144
144
145
-
# [v4.x](#tab/v4)
145
+
# [v4](#tab/v4)
146
146
147
-
v4.x only supports one way of getting the trigger input, as the first argument.
147
+
v4 only supports one way of getting the trigger input, as the first argument.
148
148
149
149
```javascript
150
150
asyncfunctionhelloWorld1(request, context) {
151
151
constonlyOption= request;
152
152
```
153
153
154
-
# [v3.x](#tab/v3)
154
+
# [v3](#tab/v3)
155
155
156
-
v3.x supports several different ways of getting the trigger input.
156
+
v3 supports several different ways of getting the trigger input.
157
157
158
158
```javascript
159
159
asyncfunctionhelloWorld1(context, request) {
@@ -169,19 +169,19 @@ async function helloWorld1(context, request) {
169
169
170
170
### Setting the primary output
171
171
172
-
# [v4.x](#tab/v4)
172
+
# [v4](#tab/v4)
173
173
174
-
v4.x only supports one way of setting the primary output, through the return value.
174
+
v4 only supports one way of setting the primary output, through the return value.
175
175
176
176
```javascript
177
177
return {
178
178
body:`Hello, ${name}!`
179
179
};
180
180
```
181
181
182
-
# [v3.x](#tab/v3)
182
+
# [v3](#tab/v3)
183
183
184
-
v3.x supports several different ways of setting the primary output.
184
+
v3 supports several different ways of setting the primary output.
185
185
186
186
```javascript
187
187
// Option 1
@@ -211,9 +211,9 @@ return {
211
211
212
212
### Create a test context
213
213
214
-
v3.x doesn't support creating an invocation context outside of the Azure Functions runtime, making it difficult to author unit tests. v4.x allows you to create an instance of the invocation context, although the information during tests isn't detailed unless you add it yourself.
214
+
v3 doesn't support creating an invocation context outside of the Azure Functions runtime, making it difficult to author unit tests. v4 allows you to create an instance of the invocation context, although the information during tests isn't detailed unless you add it yourself.
215
215
216
-
# [v4.x](#tab/v4)
216
+
# [v4](#tab/v4)
217
217
218
218
```javascript
219
219
consttestInvocationContext=newInvocationContext({
@@ -222,7 +222,7 @@ const testInvocationContext = new InvocationContext({
222
222
});
223
223
```
224
224
225
-
# [v3.x](#tab/v3)
225
+
# [v3](#tab/v3)
226
226
227
227
Not possible 😮
228
228
@@ -234,7 +234,7 @@ The http request and response types are now a subset of the [fetch standard](htt
234
234
235
235
### HttpRequest
236
236
237
-
# [v4.x](#tab/v4)
237
+
# [v4](#tab/v4)
238
238
- _**Body**_. You can access the body using a method specific to the type you would like to receive:
239
239
```javascript
240
240
constbody=awaitrequest.text();
@@ -252,7 +252,7 @@ The http request and response types are now a subset of the [fetch standard](htt
252
252
constname=request.query.get('name');
253
253
```
254
254
255
-
# [v3.x](#tab/v3)
255
+
# [v3](#tab/v3)
256
256
- _**Body**_. You can access the body in several ways, but the type returned isn't always consistent:
257
257
```javascript
258
258
// returns a string, object, or Buffer
@@ -278,7 +278,7 @@ The http request and response types are now a subset of the [fetch standard](htt
278
278
279
279
### HttpResponse
280
280
281
-
# [v4.x](#tab/v4)
281
+
# [v4](#tab/v4)
282
282
- _**Status**_:
283
283
```javascript
284
284
return { status:200 };
@@ -299,7 +299,7 @@ The http request and response types are now a subset of the [fetch standard](htt
299
299
};
300
300
```
301
301
302
-
# [v3.x](#tab/v3)
302
+
# [v3](#tab/v3)
303
303
- _**Status**_. A status can be set in several different ways:
This guide is an introduction to developing Azure Functions using JavaScript or TypeScript. The article assumes that you've already read the [Azure Functions developer guide](functions-reference.md).
16
16
17
17
> [!IMPORTANT]
18
-
> The content of this article changes based on your choice of the Node.js programming model in the selector at the top of this page. The version you choose should match the version of the [`@azure/functions`](https://www.npmjs.com/package/@azure/functions) npm package you are using in your app. If you do not have that package listed in your `package.json`, the default is v3.x. Learn more about the differences between v3.x and the new v4.x programming model in the [upgrade guide](./functions-node-upgrade-v4.md). v4.x is currently in public preview.
18
+
> The content of this article changes based on your choice of the Node.js programming model in the selector at the top of this page. The version you choose should match the version of the [`@azure/functions`](https://www.npmjs.com/package/@azure/functions) npm package you are using in your app. If you do not have that package listed in your `package.json`, the default is v3. Learn more about the differences between v3 and the new v4 programming model in the [upgrade guide](./functions-node-upgrade-v4.md). v4 is currently in public preview.
19
19
20
20
As a JavaScript developer, you might also be interested in one of the following articles:
21
21
@@ -27,7 +27,7 @@ As a JavaScript developer, you might also be interested in one of the following
27
27
28
28
The "programming model" as discussed in this article loosely represents the Node.js layer of Azure Functions. If you are authoring code in JavaScript or TypeScript, the code patterns you use are governed by the programming model. For example, it defines how you register a function, get a function's input, or interact with the invocation context. The programming model version is strictly tied to the version of the [`@azure/functions`](https://www.npmjs.com/package/@azure/functions) npm package and is _not_ the same thing as the Azure Functions [runtime version](./functions-versions.md), even if the major version numbers are coincidentally similar.
29
29
30
-
> [!WARNING]
30
+
> [!NOTE]
31
31
> You can only use one major version of the programming model at a time.
32
32
33
33
### Supported Versions
@@ -261,12 +261,12 @@ app.http('httpTrigger1', {
261
261
});
262
262
```
263
263
264
-
> [!WARNING]
265
-
> You can't mix the v3.x and v4.x programming models. As soon as you register one v4.x function in your app, any v3.x functions registered in `function.json` files will be ignored.
264
+
> [!NOTE]
265
+
> You can't mix the v3 and v4 programming models. As soon as you register one v4 function in your app, any v3 functions registered in `function.json` files will be ignored.
266
266
267
267
## Inputs and outputs
268
268
269
-
Your function is required to have exactly one primary input called the trigger. It may also have secondary inputs, a primary output called the return output, and/or secondary outputs. Inputs and outputs are also referred to as bindings outside the context of the Node.js programming model. Before v4.x of the model, these bindings were configured in `function.json` files.
269
+
Your function is required to have exactly one primary input called the trigger. It may also have secondary inputs, a primary output called the return output, and/or secondary outputs. Inputs and outputs are also referred to as bindings outside the context of the Node.js programming model. Before v4 of the model, these bindings were configured in `function.json` files.
0 commit comments