Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems when using ES6 syntax #110

Open
kevpye-fabdata opened this issue Aug 2, 2021 · 1 comment
Open

Problems when using ES6 syntax #110

kevpye-fabdata opened this issue Aug 2, 2021 · 1 comment

Comments

@kevpye-fabdata
Copy link

kevpye-fabdata commented Aug 2, 2021

Hi there,

I am trying to get a web app that is compiled to nodejs ES6 syntax to work on a Windows based Azure app service but I am having absolutely no luck so im hoping someone here can help me. First up, my web config is the same as the MS quick start demo (comments removed for verbosity) for a nodejs application - as shown below:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <webSocket enabled="false" />
    <handlers>
      <add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^index.js\/debug[\/]?" />
        </rule>

        <rule name="StaticContent">
          <action type="Rewrite" url="public{REQUEST_URI}"/>
        </rule>

        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="index.js"/>
        </rule>
      </rules>
    </rewrite>
    
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>

    <httpErrors existingResponse="PassThrough" />
  </system.webServer>
</configuration>

In the root of my application I also have a very simple package.json file with a single property of type="module", as shown below, which simply tells node to use the newer syntax (I am doing this because the bundler that builds my app spits out .js files and not .mjs.):

{ "type": "module" }
With this configuration the app runs perfectly locally by simply running node index.js from any folder containing the app.

When I deploy the app to the azure app service however the page will not load. Checking the logs I can see the following error:
require() of ES modules is not supported. require() of C:\home\site\wwwroot\index.js from C:\Program Files (x86)\iisnode\interceptor.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" 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 "type": "module" from C:\home\site\wwwroot\package.json.

I found this suggestion on stackoverflow - which basically suggests adding a .cjs file to the root which simply imports the app. Then set this as IISNodes entry point in the web config. I have tried this solution, but the app doesnt work, it simply renders the index.js files as raw text in the browser.

Locally I am running node 14.17.0. In the application settings for the app service the node version is set to 14 and running node --version in the scm / kudu site returns 14.16.0 so the node version is fine. What can I do to get this working??

@BobFrankston
Copy link

See my issue #113. You fix interecept.js and change

require(process.argv[1]);

To

require('child_process').spawn(process.arv[0], process.argv.slice(1));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants