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: README.md
+22-10
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ The goal is to make it easy to write Lambda's with transpiled JS/TypeScript feat
6
6
7
7
## New Alternative to Netlify Lambda
8
8
9
-
Netlify-Lambda uses webpack to bundle up your functions and their dependencies for you, however this is not the only approach. If you have native node modules (or other dependencies that dont expect to be bundled like [the Firebase SDK](https://github.com/netlify/netlify-lambda/issues/112)) then you may want to try the zipping approach.
9
+
Netlify-Lambda uses webpack to bundle up your functions and their dependencies for you, however this is not the only approach. If you have native node modules (or other dependencies that dont expect to be bundled like [the Firebase SDK](https://github.com/netlify/netlify-lambda/issues/112)) then you may want to try the zipping approach.
10
10
11
11
We have recently soft released a new library to do this for you: https://github.com/netlify/zip-it-and-ship-it. This is integrated into the Netlify CLI. There is [more documentation here](https://www.netlify.com/docs/cli/#unbundled-javascript-function-deploys) in the official CLI docs and support is available through [our regular channels](https://www.netlify.com/support/).
12
12
@@ -64,6 +64,7 @@ It also watches your files and restarts the dev server on change. Note: if you a
64
64
- You need a [`netlify.toml`](https://www.netlify.com/docs/netlify-toml-reference/) file with a `functions` field.
65
65
- Every function needs to be a top-level js/ts/mjs file. You can have subfolders inside the `netlify-lambda` folder, but those are only for supporting files to be imported by your top level function. Files that end with `.spec.*` or `.test.*` will be ignored so you can [colocate your tests](https://github.com/netlify/netlify-lambda/issues/99).
66
66
- Function signatures follow the [AWS event handler](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html) syntax but must be named `handler`. [We use Node v8](https://www.netlify.com/blog/2018/04/03/node.js-8.10-now-available-in-netlify-functions/) so `async` functions **are** supported ([beware common mistakes](https://serverless.com/blog/common-node8-mistakes-in-lambda/)!). Read [Netlify Functions docs](https://www.netlify.com/docs/functions/#javascript-lambda-functions) for more info.
67
+
- Functions [time out in 10 seconds](https://www.netlify.com/docs/functions/#custom-deployment-options) by default although extensions can be requested. We [try to replicate this locally](https://github.com/netlify/netlify-lambda/pull/116).
67
68
68
69
<details>
69
70
<summary><b>Environment variables in build and branch context</b></summary>
@@ -77,7 +78,6 @@ If you need local-only environment variables that you don't place in `netlify.to
77
78
78
79
</details>
79
80
80
-
81
81
<summary>
82
82
<b>Lambda function examples</b>
83
83
</summary>
@@ -116,14 +116,17 @@ export async function handler(event, context) {
116
116
117
117
## Using with `create-react-app`, Gatsby, and other development servers
118
118
119
-
### Why you need to proxy (for beginners)
119
+
<details>
120
+
<summary><b>Why you need to proxy (for beginners)</b></summary>
120
121
121
122
`react-scripts` (the underlying library for `create-react-app`) and other popular development servers often set up catchall serving for you; in other words, if you try to request a route that doesn't exist, the dev server will try to serve you `/index.html`. This is problematic when you are trying to hit a local API endpoint like `netlify-lambda` sets up for you - your browser will attempt to parse the `index.html` file as JSON. This is why you may see this error:
122
123
123
124
`Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0`
124
125
125
126
If this desribes your situation, then you need to proxy for local development. Read on. Don't worry it's easier than it looks.
126
127
128
+
</details>
129
+
127
130
### Proxying for local development
128
131
129
132
> ⚠️IMPORTANT! PLEASE READ THIS ESPECIALLY IF YOU HAVE CORS ISSUES⚠️
@@ -212,9 +215,8 @@ If you need to use additional webpack modules or loaders, you can specify an add
212
215
213
216
For example, have a file with:
214
217
215
-
216
218
```js
217
-
// webpack.functions.js
219
+
// webpack.functions.js
218
220
module.exports= {
219
221
optimization: { minimize:false }
220
222
};
@@ -273,6 +275,7 @@ There are additional CLI options:
273
275
-c --config
274
276
-p --port
275
277
-s --static
278
+
-t --timeout
276
279
```
277
280
278
281
### --config option
@@ -281,15 +284,18 @@ If you need to use additional webpack modules or loaders, you can specify an add
281
284
282
285
For example, have a file with:
283
286
284
-
285
287
```js
286
-
// webpack.functions.js
288
+
// webpack.functions.js
287
289
module.exports= {
288
290
optimization: { minimize:false }
289
291
};
290
292
```
291
293
292
-
Then specify `netlify-lambda serve --config ./webpack.functions.js`.
294
+
Then specify `netlify-lambda serve --config ./webpack.functions.js`.
295
+
296
+
### --timeout option
297
+
298
+
(This is for local dev/serving only) The default function timeout is 10 seconds. If you need to adjust this because you have requested extra timeout, pass a timeout number here. Thanks to [@naipath](https://github.com/netlify/netlify-lambda/pull/116) for this feature.
293
299
294
300
### --port option
295
301
@@ -316,9 +322,8 @@ To debug lambdas, you can use the `--inspect` flag. Additionally:
316
322
317
323
For example, have a file with:
318
324
319
-
320
325
```js
321
-
// webpack.functions.js
326
+
// webpack.functions.js
322
327
module.exports= {
323
328
optimization: { minimize:false }
324
329
};
@@ -328,6 +333,12 @@ Then specify `netlify-lambda serve --config ./webpack.functions.js`. If using VS
328
333
329
334
Netlify Functions [run in Node v8.10](https://www.netlify.com/blog/2018/04/03/node.js-8.10-now-available-in-netlify-functions/) and you may need to run the same version to mirror the environment locally. Also make sure to check that you aren't [committing one of these common Node 8 mistakes in Lambda!](https://serverless.com/blog/common-node8-mistakes-in-lambda/)
330
335
336
+
**Special warning on `node-fetch`**: `node-fetch` and webpack [currently don't work well together](https://github.com/bitinn/node-fetch/issues/450). you will have to use the default export in your code:
337
+
338
+
```js
339
+
cons fetch =require('node-fetch').default// not require('node-fetch')
340
+
```
341
+
331
342
Don't forget to search our issues in case someone has run into a similar problem you have!
332
343
333
344
## Example functions and Tutorials
@@ -367,6 +378,7 @@ All of the above are community maintained and not officially supported by Netlif
367
378
- v1.1: https://twitter.com/swyx/status/1069544181259849729 Typescript support
0 commit comments