title: Azure App Service (Windows) - Nodejs ES Module Problems with SvelteKit app
body: <p>really hoping someone can point me in the right direction with this one as i'm having no luck at all. I'm trying to host a simple nodejs sveltekit application on a Windows based azure app service, but cannot get the application to start / run.</p>
  <p>I'm using the adapter-node adapter for sveltekit to generate the build output as a self contained node app. After sveltekit generates the build output I inject a simple package.json file to the root of the build folder to instruct node to use the ESM style imports which simply contains a single property of type=&quot;module&quot;.</p>
  <p><strong>package.json</strong></p>
  <pre><code>{ 
      &quot;type&quot;: &quot;module&quot; 
  }
  </code></pre>
  <p>Lastly I also inject a web.config into the root of the build folder for use with IISNode. The web.config file used is the same as from the nodejs quickstart guide provided by MS. The web.config can be seen <a href="https://github.com/Azure-Samples/nodejs-docs-hello-world/blob/master/web.config" rel="nofollow noreferrer">here</a>.</p>
  <p>The final folder structure of the build output is simply:</p>
  <pre><code>build
  └───assets
  │   └───_app
  │       │   ...
  └───prerendered
  │   index.js
  │   package.json
  │   web.config
  </code></pre>
  <p>Locally I can take this build folder, place it anywhere on my machine and it runs perfectly by simply running:</p>
  <pre><code>node index.js
  </code></pre>
  <p><strong>The Problem</strong></p>
  <p>Even though it works perfectly locally, when I deploy the application to the Azure app service the application will not start with the browser simply displaying &quot;This page isn’t working right now&quot;.</p>
  <p>When I check the logs I see the following error:</p>
  <pre><code>Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\home\site\wwwroot\index.js
  require() of ES modules is not supported.
  require() of D:\home\site\wwwroot\index.js from D:\Program Files (x86)\iisnode\interceptor.js is an ES module file as it is a .js file whose nearest parent package.json contains &quot;type&quot;: &quot;module&quot; which defines all .js files in that package scope as ES modules.
  Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove &quot;type&quot;: &quot;module&quot; from D:\home\site\wwwroot\package.json.
  </code></pre>
  <p>The error tells me that MS's iisnode\interceptor.js is using the commonjs style require syntax and cannot import the ES module of my index.js.</p>
  <p>I found someone having a similar problem and a suggested solution <a href="https://stackoverflow.com/a/67111490/2066033">here</a>. The suggested solution is to create a new file next to my index.js file and configuring it as the app service's (or more specifically iisnode's) entry point in the web.config. The new file would be named run.cjs and only contain the following:</p>
  <pre><code>import(&quot;./index.js&quot;);
  </code></pre>
  <p>I tried this option, adding the new run.cjs file and updating the web.config to set this as IISNodes entry point:</p>
  <pre><code>&lt;add name=&quot;iisnode&quot; path=&quot;run.cjs&quot; verb=&quot;*&quot; modules=&quot;iisnode&quot;/&gt;
  </code></pre>
  <p>When I try the site after doing this I get a new problem. The site now loads but instead of seeing the app, the js from index.js renders as raw text into the browser.</p>
  <p>The azure app service WEBSITE_NODE_DEFAULT_VERSION is set to ~14 and I can see from Kudu that the version running is 14.16.0 - my local machine is 14.17.0 so the node version looks to be ok.</p>
  <p>Can anyone help??</p>
  <p>Thanks in advance</p>