Use Typescript in Azure Functions

I could not find any resources on how to run Typescript in Azure functions so I decided to create my own as I will want this in the future. If you don not care about how I did this and just wants to get started you can check out the my repo with everything you need.

First we should install the azure cli from npm:

npm install -g azure-cli

It will by default be in the Azure Resource Manager (ARM) mode. And this is where we want to be most of the time. But in this case we have to switch to the old Azure Service Management (ASM) mode. Run the following commands to switch to the correct mode and generate a deployment script.

azure config mode arm
azure site deploymentscript --functionApp

Add the following lines before “:: 2. Restore npm”

:: 2. Install npm packages and run typescript
IF EXIST "%DEPLOYMENT_TARGET%\package.json" (
pushd "%DEPLOYMENT_TARGET%"
npm install --production
IF !ERRORLEVEL! NEQ 0 goto error
popd
)

This will install the global npm packages and run the Typescript command.

Create a tsconfig.json with our desired config. I have “node_modules” in my exclude array and then I add all my function folders to the include array. If there is a smarter way to do this please let me know in the comments. We need a postinstall in our package.json that runs the Typescript tsc command. We will have to install the types for everything we need in our functions in our outermost package.json. We will need @types/node at least.

Now that everything is setup in the project we can create our function in the Azure portal and then go to “Function app settings” in the lower left corner. Then we click on “Configure continuous integration” and choose our repo.

Where to click to setup CI

That should be it. My next post should be about writing tests to test the functions locally before deploy. We should even be able to hook it up with CI and do some automatic testing before deploy.

If I have missed anything in this blogpost you should be able to find what you need in the companion repository.