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
+10-16Lines changed: 10 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,16 +9,15 @@ ms.topic: how-to
9
9
10
10
# Upgrade to version 4 of the Node.js programming model for Azure Functions
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 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).
12
+
This article discusses the differences between version 3 and version 4 of the Node.js programming model and how to upgrade an existing v3 app. If you want to create a brand new v4 app instead of upgrading an existing v3 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). This article uses "TIP" sections to highlight the most important concrete actions you should take to upgrade your app.
13
13
14
14
Version 4 was designed with the following goals in mind:
15
15
16
16
- Provide a familiar and intuitive experience to Node.js developers
17
17
- Make the file structure flexible with support for full customization
18
18
- Switch to a code-centric approach for defining function configuration
19
19
20
-
> [!NOTE]
21
-
> 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.
20
+
[!INCLUDE [Programming Model Considerations](../../includes/functions-nodejs-model-considerations.md)]
22
21
23
22
## Requirements
24
23
@@ -35,7 +34,7 @@ Version 4 of the Node.js programming model requires the following minimum versio
35
34
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.
36
35
37
36
> [!TIP]
38
-
> _**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
37
+
> 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
39
38
> ```
40
39
> npm install @azure/functions@preview
41
40
> ```
@@ -51,21 +50,18 @@ In v4 of the programming model, you can structure your code however you want. Th
51
50
- `src/functions/*.js`
52
51
53
52
> [!TIP]
54
-
> _**Action Item**_: Make sure you define a `main` field in your `package.json` file
53
+
> Make sure you define a `main` field in your `package.json` file
55
54
56
55
## Switch the order of arguments
57
56
58
57
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.
59
58
60
59
> [!TIP]
61
-
> _**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.
60
+
> 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.
62
61
63
62
## Define your function in code
64
63
65
-
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.
66
-
67
-
> [!WARNING]
68
-
> 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.
64
+
Say goodbye 👋 to `function.json` files! All of the configuration that was previously specified 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.
> _**Action Item**_: Move the config from your `function.json` file to your code. The type of the trigger will correspond to a method on the `app` object in the new model. For example, if you use an `httpTrigger` type in `function.json`, you will now call `app.http()` in your code to register the function. If you use `timerTrigger`, you will now call `app.timer()` and so on.
126
+
> Move the config from your `function.json` file to your code. The type of the trigger will correspond to a method on the `app` object in the new model. For example, if you use an `httpTrigger` type in `function.json`, you will now call `app.http()` in your code to register the function. If you use `timerTrigger`, you will now call `app.timer()` and so on.
133
127
134
128
135
129
## Review your usage of context
@@ -163,7 +157,7 @@ async function helloWorld1(context, request) {
163
157
---
164
158
165
159
> [!TIP]
166
-
> _**Action Item**_: Make sure you aren't using `context.req` or `context.bindings` to get the input.
160
+
> Make sure you aren't using `context.req` or `context.bindings` to get the input.
167
161
168
162
### Set the primary output as your return value
169
163
@@ -205,7 +199,7 @@ return {
205
199
---
206
200
207
201
> [!TIP]
208
-
> _**Action Item**_: Make sure you are always returning the output in your function handler, instead of setting it with the `context` object.
202
+
> Make sure you are always returning the output in your function handler, instead of setting it with the `context` object.
209
203
210
204
### Create a test context
211
205
@@ -329,7 +323,7 @@ The http request and response types are now a subset of the [fetch standard](htt
329
323
---
330
324
331
325
> [!TIP]
332
-
> _**Action Item**_: Update any logic using the http request or response types to match the new methods. If you are using TypeScript, you should receive build errors if you use old methods.
326
+
> Update any logic using the http request or response types to match the new methods. If you are using TypeScript, you should receive build errors if you use old methods.
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. 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.
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 v4 in the [upgrade guide](./functions-node-upgrade-v4.md).
19
19
20
20
As a JavaScript developer, you might also be interested in one of the following articles:
21
21
22
22
| Getting started | Concepts| Guided learning |
23
23
| -- | -- | -- |
24
24
| <ul><li>[Node.js function using Visual Studio Code](./create-first-function-vs-code-node.md)</li><li>[Node.js function with terminal/command prompt](./create-first-function-cli-node.md)</li><li>[Node.js function using the Azure portal](functions-create-function-app-portal.md)</li></ul> | <ul><li>[Developer guide](functions-reference.md)</li><li>[Hosting options](functions-scale.md)</li><li>[TypeScript functions](#typescript)</li><li>[Performance considerations](functions-best-practices.md)</li></ul> | <ul><li>[Create serverless applications](/training/paths/create-serverless-applications/)</li><li>[Refactor Node.js and Express APIs to Serverless APIs](/training/modules/shift-nodejs-express-apis-serverless/)</li></ul> |
25
25
26
-
## Programming model
26
+
[!INCLUDE [Programming Model Considerations](../../includes/functions-nodejs-model-considerations.md)]
27
27
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
-
30
-
> [!NOTE]
31
-
> You can only use one major version of the programming model at a time.
32
-
33
-
### Supported versions
28
+
## Supported versions
34
29
35
30
The following table shows each version of the Node.js programming model along with its supported versions of the Azure Functions runtime and Node.js.
36
31
@@ -261,9 +256,6 @@ app.http('httpTrigger1', {
261
256
});
262
257
```
263
258
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
-
267
259
## Inputs and outputs
268
260
269
261
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.
- The Node.js "programming model" should not be confused with the Azure Functions "runtime".
15
+
-_**Programming model**_: The part of Azure Functions specific to JavaScript and TypeScript that governs how you author your code. For example it determines how you register a function, get a function's input, or interact with the invocation context.
16
+
-_**Runtime**_: The central part of Azure Functions that governs underlying behavior shared across all languages.
17
+
- The Node.js programming model is versioned independently of the Azure Functions [runtime](./functions-versions.md). The programming model version is strictly tied to the version of the [`@azure/functions`](https://www.npmjs.com/package/@azure/functions) npm package. Both the runtime and the programming model use "4" as their latest major version, but that is purely a coincidence.
18
+
- Keep in mind that you can't mix the v3 and v4 programming models in the same function app. As soon as you register one v4 function in your app, any v3 functions registered in `function.json` files will be ignored.
19
+
20
+
> [!NOTE]
21
+
> Version 4 of the Node.js programming model is currently in public preview.
0 commit comments