'How can i watch for changes in my azure functions?

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.



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mmey
Solution 2
Solution 3