Skip to content Skip to sidebar Skip to footer

Azure App Service - Nodejs Es Module Error

Trying to host a simple nodejs api server on azure app service, but getting the following error when azure tries to deploy it Error [ERR_REQUIRE_ESM]: Must use import to load ES Mo

Solution 1:

This can be solved by adding a new file next to your server.js and configuring it as the app service's (or more specifically iisnode's) entry point (see your web.config).

Let's call the new file run.cjs and put only the following line into it:

import("./server.js");

The cjs file extension is important because it tells Node that this file is not a ES module, as it would expect because of "type": "module" in your package.json. This allows other CommonJS files to include our new file - namely iisnode's interceptor.js. It again imports the server.js which then runs fine as ES module.

Solution 2:

Add "type": "module" in package.json file. It work for me.

{
  "name": "module-error",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "start": "node --experimental-modules server",
    "test": "mocha"
  },
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1"
  },
  "author": "",
  "license": "ISC",
  "type": "module"
}

enter image description here

Solution 3:

Adding to what @sibbl said

  1. Just open your web.config
  2. search for server.js replace it with run.cjs

It Works! Thanks again @sibbl

Post a Comment for "Azure App Service - Nodejs Es Module Error"