4

I am using azure functions written in NODE JS. I am starting the local server with

func host start

and everything works fine. But the problem is when i change something in my azure functions - index.js file for example when i add one simple console log


module.exports = async function (context, req) {
        console.log('request came');
        context.res = {
            body: { success: true, message: null, response: context.bindings.inputDocument }
        };
};

the server is not restarted, and i need to stop the server run again func host start to see the changes which is not good. In nodejs we have nodemon, is there something to this in azure functions with which i can watch for changes without stoppint and starting the server on every change ?

what i tried

I tried adding in my host.json file

  "watchDirectories": [ "*" ] 

but without success.

Also i tried

editing the start property in the scripts in package.json file

"scripts": {
    "start": "start npm run watch & npm run start:host",
    "test": "echo \"No tests yet...\""
  },

instead

"scripts": {
    "start": "func start",
    "test": "echo \"No tests yet...\""
  },

but still no success.

I am using azure functions v2.

sdsd
  • 447
  • 3
  • 20
  • I'm not sure if it's language specific. [Documentation](https://learn.microsoft.com/en-us/azure/azure-functions/functions-host-json#watchdirectories) points to a [shared code directories link](https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#watched-directories) that's specific to C#. Same param is not listed under java-script. Though interestingly I *vaguely* recall that for python it worked for me for the main file. – Kashyap Feb 02 '21 at 21:35
  • Also, it says it automatically monitors the folder that contains "function script". Do changes to your "function script" get reloaded automatically? There is another option `watchFiles`. I know it mightn't be as convenient, but have you tried it? – Kashyap Feb 02 '21 at 21:37
  • Yes i tried it. And yes i try to watch files for the function script - index.js file. – sdsd Feb 02 '21 at 21:52
  • On my side, the situation is 'change under watchDirectories will let the function host stop instead of restart'(No matter I change the content in the file or add files.). – Cindy Pau Feb 03 '21 at 01:40
  • I have tried both local and azure, as well as nodejs, python, and C#. No effect was seen, the function host stopped. – Cindy Pau Feb 03 '21 at 01:42
  • you don't need `watch*` as this is for watching shared code.. If you have pulled the settings form your function then you may have some extra lines in the `local.settings.json` you need to remove `WEBSITE_RUN_FROM_PACKAGE` , I removed all the `WEBSITE` settings as they are not needed for local dev' – Steve Drake Apr 28 '22 at 10:18

4 Answers4

10

You can start two processes (based on @azure/functions 1.2.3):

  • Process 1: tsc -w (or npm run watch)
  • Process 2: func start (or npm start)

The Typescript compiler will then work in watch mode, compile changes automatically, and the azure func tool will also reload it's functions on changes.

mmey
  • 1,545
  • 16
  • 24
  • it says ` Starting compilation in watch mode` but the server is unreachable now. But i see that the output timestamp changes after the file change is saved. just not loading the localhost. – Giorgi Gvimradze Jun 22 '22 at 08:51
2

What you are doing is correct, but you don't need the watch as this is to watch shared code eg code in another folder that you reference.

We do the following

"watch": "tsc --w",
"start:host": "func start --javascript",
"start": "npm-run-all --parallel start:host watch",

And we start with

npm start

npm-run-all is a package, install with npm install npm-run-all --save-dev

If its not working, then its likely that you have some extra settings in your local.settings.json such as WEBSITE_RUN_FROM_PACKAGE

If you have ever run func azure functionapp fetch-app-settings then it will grab all the settings including the ones you DONT NEED, having WEBSITE_RUN_FROM_PACKAGE prevents the auto restart.

Steve Drake
  • 1,968
  • 2
  • 19
  • 41
  • Thanks for the answer. The solution worked fine. Does it mean that for production environment, we will need to change the start command in order to not start the project in watch mode? – Giorgi Gvimradze Jun 22 '22 at 08:57
  • 2
    @GiorgiGvimradze Azure just starts your function and the command in the `package.json` is not used. – Steve Drake Jul 01 '22 at 10:49
0

Eureka. I solved it in an elegant way.

Part to enable debugging mode hot reload:-

  • .vscode/launch.json
{
   "name":"Attach to Node Functions",
   "type":"node",
   "request":"attach",
   "port":9229,
   "preLaunchTask":"func: host start",
   "restart":true
}
  • .vscode/task.json
[
   {
      "type":"func",
      "command":"host start",
      "problemMatcher":"$func-node-watch",
      "isBackground":true,
      "dependsOn":"npm build"
   },
   {
      "type":"shell",
      "label":"npm build",
      "command":"npm run watch",
      "dependsOn":"npm install",
      "problemMatcher":"$tsc-watch",
      "isBackground":true
   },
   {
      "type":"shell",
      "label":"npm install",
      "command":"npm install"
   },
   {
      "type":"shell",
      "label":"npm prune",
      "command":"npm prune --production",
      "dependsOn":"npm build",
      "problemMatcher":[
         
      ]
   }
]

Here code ref: https://github.com/safwanmasarik/Azure-Function-Typescript-Hot-Reload .

Video evidence: https://user-images.githubusercontent.com/35250295/201973704-b11eeb4a-8d41-4e0b-9f9b-385fdcf10846.mp4

Git issue: https://github.com/Azure/azure-functions-core-tools/issues/1239#issuecomment-1315565942

  • not sure why this was downvoted. maybe because the OP didn't ask for a solution in visual studio code? i was looking for a solution that worked with visual studio code debugging that this did it. specifically the `restart: true` in the `.vscode/launch.json` file. the other two upvoted solutions do work outside visual studio code but do not work with the visual studio debugger `launch.json` configuration. – gabe Aug 11 '23 at 00:45
-1

the server is not restarted, and i need to stop the server run again func host start to see the changes which is not good. In nodejs we have nodemon, is there something to this in azure functions with which i can watch for changes without stoppint and starting the server on every change ?

For this question, I think the answer is no. The restart function is necessary, not only on azure, but also locally, every time the code is updated, the runtime will be restarted (it will download and load some dependency packages when restarting, and then start the entire azure function runtime).

On my side, the situation is 'change under watchDirectories will let the function host stop instead of restart'(No matter I change the content in the file or add files.) When on azure, it is similar. The function restart after the content changes.

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27