skip to Main Content

How can the Azure Function version be upgraded from V3 to V4 when both JavaScript and TypeScript files are maintained in the same folder structure? Below is the folder structure for the V3 model.
[folder structure in azure function v3 model]

<project_root>/
 | - .vscode/
 | - dist/
 | - node_modules/
 | - myFirstFunction/
 | | - index.ts
 | | - function.json
 | - mySecondFunction/
 | | - index.ts
 | | - function.json
 | - myThirdFunction/
 | | - index.js
 | | - function.json
 | - .funcignore
 | - host.json
 | - local.settings.json
 | - package.json
 | - tsconfig.json

Additionally, how should the path value for the **main **key in the package.json file be specified for the Azure Function V4 model when both JavaScript and TypeScript files are present in the same folder?

"main": "dist/src/functions/*.js"

Tried building the root folder and typescript files are converted to JavaScript and moved to dist folder but javascript files are stored in the same folder. After build is generated, how to run the converted JS files from dist folder and jas files from root folder.

2

Answers


  1. Azure functions V4 model of the Node.js Azure functions requires the versions, refer MSDOC:

    @azure/functions - package v4.*.*
    Azure Functions Runtime - v4.25+
    Azure Functions Core Tools - v4.*.*
    

    host.json:

    {
      "version": "2.0",
      "logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          }
        }
      },
      "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[4.*, 5.0.0)"
      }
    }
    

    Switch (context, request) to either (request, context) or just (request) if you aren’t using the context in function code.

    Javascript:

    const { app } = require('@azure/functions');
    
    app.http('httpTrigger1', {
        methods: ['GET', 'POST'],
        authLevel: 'anonymous',
        handler: async (request, context) => {
            context.log(`Http function processed request for url "${request.url}"`);
    
            const name = request.query.get('name') || await request.text() || 'world';
    
            return { body: `Hello, ${name}!` };
        }
    });
    

    TypeScript:

    import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
    
    export async function httpTrigger(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
        context.log(`Http function processed request for url "${request.url}"`);
    
        const name = request.query.get('name') || await request.text() || 'world';
    
        return { body: `Hello, ${name}!` };
    };
    
    app.http('httpTrigger', {
        methods: ['GET', 'POST'],
        authLevel: 'anonymous',
        handler: httpTrigger
    });
    

    .vscode/Settings.json:

    {
        "azureFunctions.deploySubpath": ".",
        "azureFunctions.postDeployTask": "npm install (functions)",
        "azureFunctions.projectLanguage": "TypeScript",
        "azureFunctions.projectRuntime": "~4",
        "debug.internalConsoleOptions": "neverOpen",
        "azureFunctions.projectLanguageModel": 4,
        "azureFunctions.preDeployTask": "npm prune (functions)"
    }
    
    • function.json file is no longer required for each trigger you have in the V4 model as bindings are configured in code by default.
    • The application setting "AzureWebJobsFeatureFlags": "EnableWorkerIndexing" is no longer required to get a v4 model function working.

    how to run the converted JS files from dist folder and jas files from root folder.

    The model loads the functions based on the main field provided in package.json. You have to mention the entry point of either JavaScript or Typescript function using main field.

    Javascript:

    src/index.js
    src/functions/*.js
    src/{index.js,functions/*.js}
    

    TypeScript:

    dist/src/index.js
    dist/src/functions/*.js
    dist/src/{index.js,functions/*.js}
    

    I have a function project with both Javascript and Typescript Azure functions.

    Package.json:

    {
      "name": "",
      "version": "1.0.0",
      "description": "",
      "main": "dist/src/functions/*.js",
      "scripts": {
        "build": "tsc",
        "watch": "tsc -w",
        "clean": "rimraf dist",
        "prestart": "npm run clean && npm run build",
        "start": "func start",
        "test": "echo "No tests yet...""
      },
      "dependencies": {
        "@azure/functions": "^4.0.0"
      },
      "devDependencies": {
        "azure-functions-core-tools": "^4.x",
        "@types/node": "18.x",
        "typescript": "^4.0.0",
        "rimraf": "^5.0.0"
      }
    }
    

    The model loaded the typescript functions as I have configured main field to load the functions present in the dist folder.

    [2024-11-14T09:40:04.469Z] Worker process started and initialized.
    
    Functions:
    
            httpTrigger1: [GET,POST] http://localhost:7071/api/httpTrigger1
    
            httpTrigger2: [GET,POST] http://localhost:7071/api/httpTrigger2
    
    For detailed output, run func with --verbose flag.
    [2024-11-14T09:40:09.393Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
    

    Update main field to "src/functions/*.js" in Package.json to run the JavaScript function.

    [2024-11-14T10:01:14.867Z] Worker process started and initialized.
    
    Functions:
    
            httpTrigger: [GET,POST] http://localhost:7071/api/httpTrigger
    
    For detailed output, run func with --verbose flag.
    [2024-11-14T10:01:27.902Z] Executing 'Functions.httpTrigger' (Reason='This function was programmatically called via the host APIs.', Id=478aa6d6-00b3-400d-a328-abefd658e4fe)
    
    Login or Signup to reply.
  2. To upgrade from Azure Functions V3 to V4 while maintaining both JavaScript and TypeScript files in the same folder structure, you need to make a few changes:

    1. Upgrade Dependencies: Update your package.json to use the latest Azure Functions SDK, such as "@azure/functions": "^4.x.x".

    2. Folder Structure: Ensure that TypeScript files are compiled to the dist/ folder, and JavaScript files remain in the root folder. The tsconfig.json should be set to output files in dist/, for example:

      "outDir": "./dist"
      
    3. Update package.json: For the main key, you can specify the entry point for the compiled JavaScript functions as:

      "main": "dist/index.js"
      
    4. Run the Functions: After building the TypeScript code, you can run the converted JavaScript files from the dist folder using the Azure Functions runtime. If you are running locally, use func start and ensure your local.settings.json is correctly configured. For JavaScript functions in the root, you can use them as usual, but ensure they are correctly referenced in the function.json.

    In summary, after building, the TypeScript files will be in dist/, and the JavaScript files will be in the root directory, with the entry point properly set in package.json.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search