node.jstypescriptazure-functionsmonorepoazure-functions-runtime

Nodejs Monorepo with Azure Functions fails at runtime for func start


I have a nodejs typescript monorepo set up in the below structure

libraries/
  lib1/
  lib2/
  package.json
  tsconfig.json
web-api/
  function1/
    function.json
    index.ts
  function2/
    function.json
    index.ts
  host.json
  package.json
  tsconfig.json
package.json
tsconfig.json

it is currently configured with workspaces in the root's package.json, and it builds fine if I run npm run build --workspace=web-api, however when I cd into web-api and try to run func start it throws a runtime error Worker was unable to load function function1: 'Cannot find module 'libraries/lib1/constants'

my nested tsconfig is set up to reference the libraries folder, and when web-api workspace is built, I see a dist folder with both libraries and web-api subfolders, so it pulls in all of the relevant js and mapping files that tsc builds, but for some reason func start is looking somewhere else. Is that additional plumbing that I need? Or am I executing it wrong?

UPDATE: link to a github repo that can reproduce the issue: https://github.com/jobeland/functionrepro

Steps:

  1. navigate to root
  2. npm install
  3. npm run build --workspace=functionapp1
  4. cd functionapp1
  5. func start

expectation: function app runs successfully and http trigger is accessible actual: function app fails to run because it can't find libraries folder.


Solution

  • I got the same error when I ran the code you have shared.

    Resolution:

    Modify the import statement in functionapp1/index.js:

    Use

    const  constants  =  require('../../libraries/common/constants');
    

    instead of

    import  *  as  constants  from  "libraries/common/constants";
    

    I'm able to import the libraries and run the function successfully:

    C:\Users\uname\customlib\functionrepro>npm run build
    
    > functionrepro@1.0.0 build
    > tsc
    
    C:\Users\uname\functionrepro\functionapp1>npm start
    
    > functionapp1@1.0.0 prestart
    > npm run build
    
    > functionapp1@1.0.0 build
    > tsc
    
    > functionapp1@1.0.0 start
    > func start
    
    Azure Functions Core Tools
    Core Tools Version:       4.0.5700 Commit hash: N/A +71cc84964a60bfb07d95839b7c666bd239507bdd (64-bit)
    Function Runtime Version: 4.33.2.22572
    
    [2024-06-17T13:15:31.642Z] Worker process started and initialized.
    
    Functions:
    
            HttpTriggerGreeting: [GET] http://localhost:7071/api/greeting
    
    For detailed output, run func with --verbose flag.
    [2024-06-17T13:15:36.401Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
    [2024-06-17T13:15:36.806Z] Executing 'Functions.HttpTriggerGreeting' (Reason='This function was programmatically called via the host APIs.', Id=11f7b13f-a6d9-4aee-91ce-c55a53c3c965)
    [2024-06-17T13:15:37.068Z] Executed 'Functions.HttpTriggerGreeting' (Succeeded, Id=11f7b13f-a6d9-4aee-91ce-c55a53c3c965, Duration=296ms)
    

    Output:

    enter image description here