Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Prepare applications for Azure deployment by generating infrastructure code, Dockerfiles, and config files.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/services/functions/templates/recipes/common/nodejs-entry-point.md
1# Node.js Entry Point (REQUIRED)23Azure Functions Node.js v4 programming model requires an entry point file that initializes the runtime.45> ⛔ **CRITICAL**: Without this file, functions will deploy but return 404 on all endpoints.67## Project Structure (CRITICAL)89The project structure MUST be:1011```12project-root/ # ← azure.yaml project: "."13├── azure.yaml14├── package.json # ← MUST be at ROOT, not in src/15├── host.json16├── src/17│ ├── index.js # ← Entry point (app.setup)18│ └── functions/19│ ├── myFunction.js # ← Functions auto-discovered20│ └── ...21└── infra/22```2324> ⛔ **CRITICAL**: `package.json` MUST be at project root, NOT inside `src/`.25> The `azure.yaml` must have `project: .` (not `project: ./src/`).2627## JavaScript: src/index.js2829**This file MUST exist and MUST NOT be removed when replacing trigger files.**3031```javascript32const { app } = require('@azure/functions');3334app.setup({35enableHttpStream: true,36});37```3839## TypeScript: src/index.ts4041**This file MUST exist and MUST NOT be removed when replacing trigger files.**4243```typescript44import { app } from '@azure/functions';4546app.setup({47enableHttpStream: true,48});49```5051## package.json Configuration5253The `package.json` MUST be at the project root (same level as `azure.yaml`).5455> ⛔ **CRITICAL: The glob pattern is REQUIRED for function discovery!**56> Using `"main": "src/index.js"` alone will result in 404 on all endpoints.57> You MUST use the glob pattern that includes function files.5859### JavaScript (REQUIRED pattern)60```json61{62"main": "src/{index.js,functions/*.js}",63"scripts": {64"start": "func start"65}66}67```6869### TypeScript (REQUIRED pattern)70```json71{72"main": "dist/src/{index.js,functions/*.js}",73"scripts": {74"build": "tsc",75"prestart": "npm run build",76"start": "func start"77}78}79```8081> **Why the Glob Pattern is Required**: The Azure Functions Node.js v4 runtime uses the `main` field to determine which files to load. Unlike CommonJS where `require()` chains work, the runtime needs ALL function files explicitly listed in the glob pattern. Without the glob, only `index.js` loads and functions in `src/functions/` are never registered.82>83> ❌ **WRONG**: `"main": "src/index.js"` — Functions not discovered, 404 on all routes84> ✅ **CORRECT**: `"main": "src/{index.js,functions/*.js}"` — All functions discovered8586## azure.yaml Configuration8788```yaml89services:90api:91project: . # ← ROOT directory, not ./src/92language: js # or ts for TypeScript93host: function94```9596> ⛔ **CRITICAL**: Use `project: .` — NOT `project: ./src/`. The runtime expects `package.json` at the project root.9798## Build Requirements (TypeScript only)99100Before deployment, TypeScript must be compiled:101102```bash103npm run build104```105106This outputs JavaScript to `dist/` which is what Azure Functions actually runs.107108## Common Mistakes109110| Mistake | Symptom | Fix |111|---------|---------|-----|112| **Using `"main": "src/index.js"` without glob** | **404 on all endpoints** | **Use `"main": "src/{index.js,functions/*.js}"`** |113| Missing `src/index.js` | 404 on all endpoints | Add the entry point file with `app.setup()` |114| Deleting `src/index.js` when replacing triggers | 404 after recipe applied | Keep index.js, only replace function files |115| `package.json` in `src/` instead of root | 404, functions not found | Move `package.json` to project root |116| `project: ./src/` in azure.yaml | Deployment fails or 404 | Use `project: .` |117| Missing `npm run build` for TypeScript | 404 or old code runs | Run build before deploy |118119> ⛔ **#1 CAUSE OF 404 ERRORS**: Using `"main": "src/index.js"` instead of the glob pattern.120> The glob `src/{index.js,functions/*.js}` is **REQUIRED** — it's not optional!121122## Terraform vs Bicep: Source Code is IDENTICAL123124The Node.js source code and `package.json` are **exactly the same** for both IaC types.125126Only the `infra/` folder differs:127- Bicep: `infra/*.bicep`128- Terraform: `infra/*.tf`129130> ⚠️ If you find yourself changing imports or source code because of IaC choice, something is wrong. The application code should be IaC-agnostic.131