Skip to content

Error "Must use import to load ES Module" when using es6 modules #90

Open

Description

@kerteszgab

I get the error

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Program Files\iisnode\www\es6module\runner.mjs

when I run the example below under iisnode:

hi.mjs:

export function sayHi(name) {
    return "Hi, " + name + "!"
}

runner.mjs:

import { sayHi } from './hi.mjs'
console.log(sayHi('LogRocket'))

It all works as expected when run with
node runner.mjs
(node 13.8.0), however under iisnode I get the error above.

Here is my web.config for reference:

<configuration>
  <system.webServer>

    <handlers>
      <add name="iisnode" path="runner.mjs" verb="*" modules="iisnode" />
    </handlers>

    <iisnode
      nodeProcessCommandLine="&quot;%programfiles%\nodejs\node.exe&quot;"
      interceptor="&quot;%programfiles%\iisnode\interceptor.js&quot;" />

  </system.webServer>
</configuration>

Activity

  1. patrickgalbraith commented on Dec 9, 2020

    @patrickgalbraith

    Having this problem as well.

  2. patrickgalbraith commented on Dec 10, 2020

    @patrickgalbraith

    I managed to fix this issue by using the esm module. I created a server.js file which I reference from the web.config like this:

    require = require("esm")(module/*, options*/);
    module.exports = require("./app.js");

    In this example app.js is an ES module.

  3. Ben555555 commented on Nov 20, 2024

    @Ben555555

    Patrick Galbraith (@patrickgalbraith)
    I'm still getting the following exception:

    Application has thrown an uncaught exception and is terminated:
    Error: Cannot find module 'esm'
    Require stack:
    - D:\wwwroot\sites\...\server.js
    - C:\Program Files\iisnode\interceptor.js
        at Module._resolveFilename (node:internal/modules/cjs/loader:1039:15)
        at Module._load (node:internal/modules/cjs/loader:885:27)
        at Module.require (node:internal/modules/cjs/loader:1105:19)
        at require (node:internal/modules/cjs/helpers:103:18)
        at Object.<anonymous> (D:\wwwroot\sites\IFJ-Levante-App2\server.js:1:11)
        at Module._compile (node:internal/modules/cjs/loader:1218:14)
        at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
        at Module.load (node:internal/modules/cjs/loader:1081:32)
        at Module._load (node:internal/modules/cjs/loader:922:12)
        at Module.require (node:internal/modules/cjs/loader:1105:19)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions