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/create-first-function-cli-node.md
+101-4Lines changed: 101 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,21 @@
1
1
---
2
2
title: Create a JavaScript function from the command line - Azure Functions
3
3
description: Learn how to create a JavaScript function from the command line, then publish the local Node.js project to serverless hosting in Azure Functions.
In this article, you use command-line tools to create a JavaScript function that responds to HTTP requests. After testing the code locally, you deploy it to the serverless environment of Azure Functions.
15
14
16
-
Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.
15
+
>[!NOTE]
16
+
>The v4 programming model for authoring Functions in Node.js is currently in Preview. Compared to the current v3 model, the v4 model is designed to have a more idiomatic and intuitive experience for JavaScript and TypeScript developers. To learn more, see the [Developer Reference Guide](functions-reference-node.md).
17
+
18
+
Use the selector at the top to choose the programming model of your choice for completing this quickstart. Note that completion will incur a small cost of a few USD cents or less in your Azure account.
17
19
18
20
There is also a [Visual Studio Code-based version](create-first-function-vs-code-node.md) of this article.
19
21
@@ -23,31 +25,55 @@ Before you begin, you must have the following:
23
25
24
26
+ An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio).
25
27
28
+
::: zone pivot="nodejs-model-v3"
26
29
+ The [Azure Functions Core Tools](./functions-run-local.md#v2) version 4.x.
30
+
::: zone-end
31
+
32
+
::: zone pivot="nodejs-model-v4"
33
+
+ The [Azure Functions Core Tools](./functions-run-local.md#v2) version v4.0.5085 or above
34
+
::: zone-end
27
35
28
36
+ One of the following tools for creating Azure resources:
29
37
30
38
+[Azure CLI](/cli/azure/install-azure-cli) version 2.4 or later.
31
39
32
40
+ The Azure [Az PowerShell module](/powershell/azure/install-az-ps) version 5.9.0 or later.
33
41
42
+
::: zone pivot="nodejs-model-v3"
34
43
+[Node.js](https://nodejs.org/) version 18 or 16.
44
+
::: zone-end
45
+
46
+
::: zone pivot="nodejs-model-v4"
47
+
+[Node.js](https://nodejs.org/) version 18 or above.
48
+
::: zone-end
35
49
36
50
### Prerequisite check
37
51
38
52
Verify your prerequisites, which depend on whether you are using Azure CLI or Azure PowerShell for creating Azure resources:
39
53
40
54
# [Azure CLI](#tab/azure-cli)
41
55
56
+
::: zone pivot="nodejs-model-v3"
42
57
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools are version 4.x.
58
+
::: zone-end
59
+
60
+
::: zone pivot="nodejs-model-v4"
61
+
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools are version v4.0.4915 or above.
62
+
::: zone-end
43
63
44
64
+ Run `az --version` to check that the Azure CLI version is 2.4 or later.
45
65
46
66
+ Run `az login` to sign in to Azure and verify an active subscription.
47
67
48
68
# [Azure PowerShell](#tab/azure-powershell)
49
69
70
+
::: zone pivot="nodejs-model-v3"
50
71
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools are version 4.x.
72
+
::: zone-end
73
+
74
+
::: zone pivot="nodejs-model-v4"
75
+
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools are version v4.0.4915 or above.
76
+
::: zone-end
51
77
52
78
+ Run `(Get-Module -ListAvailable Az).Version` and verify version 5.0 or later.
53
79
@@ -59,6 +85,7 @@ Verify your prerequisites, which depend on whether you are using Azure CLI or Az
59
85
60
86
In Azure Functions, a function project is a container for one or more individual functions that each responds to a specific trigger. All functions in a project share the same local and hosting configurations. In this section, you create a function project that contains a single function.
61
87
88
+
::: zone pivot="nodejs-model-v3"
62
89
1. Run the `func init` command, as follows, to create a functions project in a folder named *LocalFunctionProj* with the specified runtime:
63
90
64
91
```console
@@ -81,6 +108,8 @@ In Azure Functions, a function project is a container for one or more individual
81
108
82
109
`func new` creates a subfolder matching the function name that contains a code file appropriate to the project's chosen language and a configuration file named *function.json*.
83
110
111
+
You may find the [Azure Functions Core Tools reference](functions-core-tools-reference.md) helpful.
112
+
84
113
### (Optional) Examine the file contents
85
114
86
115
If desired, you can skip to [Run the function locally](#run-the-function-locally) and examine the file contents later.
@@ -101,6 +130,52 @@ For an HTTP trigger, the function receives request data in the variable `req` as
101
130
102
131
Each binding requires a direction, a type, and a unique name. The HTTP trigger has an input binding of type [`httpTrigger`](functions-bindings-http-webhook-trigger.md) and output binding of type [`http`](functions-bindings-http-webhook-output.md).
103
132
133
+
::: zone-end
134
+
135
+
::: zone pivot="nodejs-model-v4"
136
+
1. Run the `func init` command, as follows, to create a functions project in a folder named *LocalFunctionProj*:
137
+
138
+
```console
139
+
func init LocalFunctionProj --model V4
140
+
```
141
+
You are then prompted to choose a worker runtime and a language - choose Node for the first and JavaScript for the second.
142
+
143
+
2. Navigate into the project folder:
144
+
145
+
```console
146
+
cd LocalFunctionProj
147
+
```
148
+
149
+
This folder contains various files for the project, including configurations files named *local.settings.json* and *host.json*. Because *local.settings.json* can contain secrets downloaded from Azure, the file is excluded from source control by default in the *.gitignore* file.
150
+
151
+
3. Add a function to your project by using the following command:
152
+
153
+
```console
154
+
func new
155
+
```
156
+
157
+
Choose the template for "HTTP trigger". You can keep the default name (*httpTrigger*) or give it a new name (*HttpExample*). Your function name must be unique, or you'll be asked to confirm if your intention is to replace an existing function.
158
+
159
+
You can find the function you added in the *src/functions* directory.
160
+
161
+
4. Add Azure Storage connection information in *local.settings.json*.
@@ -129,6 +204,28 @@ Each binding requires a direction, a type, and a unique name. The HTTP trigger h
129
204
130
205
This command creates a function app running in your specified language runtime under the [Azure Functions Consumption Plan](consumption-plan.md), which is free for the amount of usage you incur here. The command also provisions an associated Azure Application Insights instance in the same resource group, with which you can monitor your function app and view logs. For more information, see [Monitor Azure Functions](functions-monitoring.md). The instance incurs no costs until you activate it.
131
206
207
+
::: zone pivot="nodejs-model-v4"
208
+
## Update app settings
209
+
210
+
To enable your V4 programming model app to run in Azure, you need to add a new application setting named `AzureWebJobsFeatureFlags` with a value of `EnableWorkerIndexing`. This setting is already in your local.settings.json file.
211
+
212
+
Run the following command to add this setting to your new function app in Azure. Replace `<FUNCTION_APP_NAME>` and `<RESOURCE_GROUP_NAME>` with the name of your function app and resource group, respectively.
213
+
214
+
# [Azure CLI](#tab/azure-cli)
215
+
216
+
```azurecli
217
+
az functionapp config appsettings set --name <FUNCTION_APP_NAME> --resource-group <RESOURCE_GROUP_NAME> --settings AzureWebJobsFeatureFlags=EnableWorkerIndexing
0 commit comments