<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dzulqarnain Nasir</title>
    <description>The latest articles on DEV Community by Dzulqarnain Nasir (@dnasir).</description>
    <link>https://dev.to/dnasir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F169541%2Fa6dbc4ea-9d3e-4e63-bf27-46d24d8403fc.jpg</url>
      <title>DEV Community: Dzulqarnain Nasir</title>
      <link>https://dev.to/dnasir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dnasir"/>
    <language>en</language>
    <item>
      <title>Azure Functions + Node.js + TypeScript + Webpack</title>
      <dc:creator>Dzulqarnain Nasir</dc:creator>
      <pubDate>Mon, 20 May 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/dnasir/azure-functions-node-js-typescript-webpack-a3m</link>
      <guid>https://dev.to/dnasir/azure-functions-node-js-typescript-webpack-a3m</guid>
      <description>&lt;p&gt;I’m a big TypeScript fan. If my blog posts and GitHub projects haven’t already made that clear, I’m now putting that on record.&lt;/p&gt;

&lt;p&gt;So when I found out that the latest &lt;code&gt;azure-functions-core-tools&lt;/code&gt; now comes with the ability to create a Functions app in TypeScript out of the box, I got excited.&lt;/p&gt;

&lt;p&gt;In this post, I’d like to share my experience setting up an Azure Functions App project for Node.js in TypeScript, with Webpack for generating the app bundles.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;You’ll first need to install &lt;code&gt;azure-functions-core-tools&lt;/code&gt; globally. This is not absolutely necessary though, since you can also install it to an empty folder, and execute commands using the &lt;code&gt;npx&lt;/code&gt; helper. That’s what I did, since my primary project was still running on the Azure Functions 1.x runtime, and I didn’t want to change anything at the time.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;func init&lt;/code&gt; to start the wizard. This will install the project to the current directory.&lt;/li&gt;
&lt;li&gt;Set the worker runtime to &lt;code&gt;node&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Set the language to &lt;code&gt;typescript&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Congratulations. You have just created an Azure Functions project in TypeScript.&lt;/p&gt;

&lt;p&gt;Under the hood, it’s using &lt;code&gt;tsc&lt;/code&gt; to compile the TypeScript files, which gets placed in the &lt;code&gt;dist&lt;/code&gt; folder. The &lt;code&gt;function.json&lt;/code&gt; has been set up to point to the compiled code using the recently added &lt;code&gt;scriptFile&lt;/code&gt; property that tells the Azure Functions runtime where to find the code when it runs the Function app.&lt;/p&gt;

&lt;p&gt;That’s it, right? Job well done, right? Er, no.&lt;/p&gt;

&lt;h2&gt;
  
  
  Paths/Aliases
&lt;/h2&gt;

&lt;p&gt;As my Functions project got larger, I started having to share code across multiple Functions, and so I started using the &lt;code&gt;paths&lt;/code&gt; property in my &lt;code&gt;tsconfig.json&lt;/code&gt; file to handle module aliasing.&lt;/p&gt;

&lt;p&gt;But as soon as I replaced &lt;code&gt;../../common/sharedModule&lt;/code&gt; with &lt;code&gt;common/sharedModule&lt;/code&gt;, my Functions apps stopped working. &lt;code&gt;Unable to find module&lt;/code&gt;, it said.&lt;/p&gt;

&lt;p&gt;Turns out, &lt;code&gt;tsc&lt;/code&gt; does not handle module resolution. It does not transform aliases to their corresponding paths, and the TypeScript team has no intention to implement this feature, as described in this &lt;a href="https://github.com/Microsoft/TypeScript/issues/10866" rel="noopener noreferrer"&gt;old GitHub issue&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Solution?&lt;/p&gt;

&lt;h2&gt;
  
  
  Webpack All The Things!
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjixfd0jvw8o6jey45aik.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjixfd0jvw8o6jey45aik.jpg" alt="Meme image of Webpack All The Things!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can use other bundlers too, if you want. I just like Webpack, so I’m going to use that for this project.&lt;/p&gt;

&lt;p&gt;Firstly, you’ll need &lt;a href="https://www.npmjs.com/package/webpack" rel="noopener noreferrer"&gt;Webpack&lt;/a&gt; (duh), and &lt;a href="https://www.npmjs.com/package/ts-loader" rel="noopener noreferrer"&gt;&lt;code&gt;ts-loader&lt;/code&gt;&lt;/a&gt; to transpile TypeScript code to regular JavaScript.&lt;/p&gt;

&lt;p&gt;Next, you’ll need to add a &lt;code&gt;webpack.config.js&lt;/code&gt; to your project.&lt;/p&gt;

&lt;p&gt;Here’s a sample configuration file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const path = require('path');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');

module.exports = {
  target: 'node', // IMPORTANT!
  entry: {
    QueueTrigger: path.resolve(__dirname, './QueueTrigger/index.ts')
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
    plugins: [
      new TsconfigPathsPlugin()
    ]
  },
  output: {
    filename: '[name]/index.js',
    path: path.resolve(__dirname, 'dist'),
    libraryTarget: 'commonjs' // IMPORTANT!
  }
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;target&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Ensure the output &lt;code&gt;target&lt;/code&gt; is set to &lt;code&gt;node&lt;/code&gt; to ensure that the code is compiled to work in a Node.js environment. The default value is &lt;code&gt;web&lt;/code&gt;, and certain libraries, such as &lt;a href="https://www.npmjs.com/package/axios" rel="noopener noreferrer"&gt;Axios&lt;/a&gt;, depend on this to determine which version of the code it should load up during compilation, and using the wrong code has some dire consequences, as I have come to discover.&lt;/p&gt;

&lt;p&gt;My project depended on Axios, and prior to setting the build target to &lt;code&gt;node&lt;/code&gt;, my Functions app kept throwing this error about &lt;code&gt;XMLHttpRequest&lt;/code&gt; not being defined. This is because Webpack built the project for the web environment where &lt;code&gt;XMLHttpRequest&lt;/code&gt; actually exists, and not for the Node environment, where it doesn’t.&lt;/p&gt;

&lt;p&gt;Setting the build target to &lt;code&gt;node&lt;/code&gt; fixed this problem.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;libraryTarget&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;You’ll need to set the output &lt;code&gt;libraryTarget&lt;/code&gt; to &lt;code&gt;commonjs&lt;/code&gt; to ensure the entry point’s default export is the output module’s default export. Without this, you’ll get the following error when you run your Azure Functions app.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

[error] Worker was unable to load function QueueTrigger: 'Unable to determine function entry point. If multiple functions are exported, you must indicate the entry point, either by naming it 'run' or 'index', or by naming it explicitly via the 'entryPoint' metadata property.'


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;tsconfig-paths-webpack-plugin&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The next part is optional, but crucial for my situation. I wanted to use aliases, so I can avoid having to resolve module paths using relative paths. To this end, I went with &lt;a href="https://www.npmjs.com/package/tsconfig-paths-webpack-plugin" rel="noopener noreferrer"&gt;&lt;code&gt;tsconfig-paths-webpack-plugin&lt;/code&gt;&lt;/a&gt; to use the aliases I’ve set up in my &lt;code&gt;tsconfig.json&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Alternatively, you can also set this up in your &lt;code&gt;webpack.config.js&lt;/code&gt; file like so:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

module.exports = {
  resolve: {
    alias: {
      'common': path.resolve(__dirname, './common')
    }
  },
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;But who would want to do that anyway, right?&lt;/p&gt;

&lt;h3&gt;
  
  
  Update project NPM scripts
&lt;/h3&gt;

&lt;p&gt;Finally, you will want to update the NPM scripts defined in the project &lt;code&gt;package.json&lt;/code&gt; file, specifically the &lt;code&gt;build&lt;/code&gt; and &lt;code&gt;watch&lt;/code&gt; scripts.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
  "scripts": {
    "build": "webpack --mode=production",
    "watch": "webpack --mode=development --watch"
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So the next time you run &lt;code&gt;npm run start&lt;/code&gt;, it will start up the Azure Functions project, and at the same time run the Webpack watcher.&lt;/p&gt;

&lt;p&gt;It should be noted that in order for the changes to be picked up by the runtime, you’ll need to make sure that the &lt;code&gt;dist&lt;/code&gt; folder is added to the watch list. This is done in the &lt;code&gt;host.json&lt;/code&gt; file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
  "watchDirectories": [
    "dist"
  ]
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now every time Webpack rebuilds the code and updates the files in the &lt;code&gt;dist&lt;/code&gt; folder, the Azure Functions runtime will detect this, and proceeds to restart the Functions app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deployment
&lt;/h2&gt;

&lt;p&gt;There are a number of ways you could go about deploying your Functions App to Azure, as described in the &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/" rel="noopener noreferrer"&gt;official Azure Functions documentation&lt;/a&gt;, under How-to Guides &amp;gt; Deploy. I went with the &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-continuous-deployment" rel="noopener noreferrer"&gt;Continuous Deployment&lt;/a&gt; option from GitHub.&lt;/p&gt;

&lt;p&gt;After setting up my deployment configuration, Azure automatically ran the deployment job. Awesome.&lt;/p&gt;

&lt;p&gt;Unfortunately, at the end of the deployment process, I found myself with an Azure Functions App that had no jobs.&lt;/p&gt;

&lt;p&gt;What gives, Azure?&lt;/p&gt;

&lt;p&gt;Turns out that while the documentation states that &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#dependency-management" rel="noopener noreferrer"&gt;&lt;code&gt;npm install&lt;/code&gt; will be triggered when a &lt;code&gt;package.json&lt;/code&gt; file is found&lt;/a&gt;, Azure does not automatically run the build job. According to their documentation, &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#typescript" rel="noopener noreferrer"&gt;transpilation is done before the host is initialized and during the deployment process&lt;/a&gt;. I suspect that the transpilation process probably failed for the same reason mentioned above.&lt;/p&gt;

&lt;p&gt;Solution? NPM scripts.&lt;/p&gt;

&lt;p&gt;While I haven’t found a way to manually invoke an NPM command during the deployment process, I do know that &lt;a href="https://docs.npmjs.com/misc/scripts" rel="noopener noreferrer"&gt;NPM has multiple events&lt;/a&gt; I can hook into when &lt;code&gt;npm install&lt;/code&gt; is called. So I decided to hook onto the &lt;code&gt;postinstall&lt;/code&gt; event to call my build task.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
  "scripts": {
    "postinstall": "npm run build",
    "build": "webpack --mode=production"
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This ensures the project is built during deployment, and sure enough, I now see my new Azure Functions listed under the Functions list.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;It’s awesome that Azure functions now support TypeScript out of the box. However, while their current implementation is probably good enough for simple apps, they are still missing a few functionalities, most notably aliasing. Fortunately, by utilising a combination of webpack and the NPM script pipeline, I was able to overcome these obstacles.&lt;/p&gt;

&lt;p&gt;I hope this helps anyone else facing the same issue.&lt;/p&gt;

</description>
      <category>azurefunctions</category>
      <category>node</category>
      <category>typescript</category>
      <category>webpack</category>
    </item>
  </channel>
</rss>
