javascript - Azure App Service - Nodejs ES Module Error - Stack Overflow

admin2025-04-16  0

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 Module: D:\home\site\wwwroot\server.js
require() of ES modules is not supported.
require() of D:\home\site\wwwroot\server.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 "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename server.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from D:\home\site\wwwroot\package.json.

Checked the azure app service, WEBSITE_NODE_DEFAULT_VERSION is set to ~14, and the installed nodejs version is v14.15.0 in the web app. Don't think this version of node has problem with import export anymore.

The code runs perfectly fine locally with either

node server.js 
# or
node --experimental-modules server

Not sure why it is failing in azure

My code is below:

server.js

import express from 'express';
import bodyParser from 'body-parser';

let app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

let port = process.env.PORT || 8080;
let router = express.Router(); 

// Middleware to use for all requests
router.use(function(req, res, next) {
    next();
});

router.get('/', function(req, res) {
    res.json({ message: 'Default home page for the api!' });
});

app.use('/api', router);

// START THE SERVER
// =============================================================================
app.listen(port);

console.log(`Server up and running on port ${port}`);

package.json

{
    "name": "my_package",
    "version": "1.0.0",
    "type": "module",
    "scripts": {
      "start": "node --experimental-modules server",
      "test": "mocha"
    },
    "dependencies": {
      "body-parser": "^1.19.0",
      "express": "^4.17.1"
    }
  }
  

However, if I change the import & export to require, then it will run in azure web app, it seems like perhaps azure iisnode is not patible with emac6 yet? Anyone knows?

Anyone has any work around of this beside using babel to transpile emac6 down to emac5? As I have having some problem with executing and running the transpiled emac5 code.

Thankz

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 Module: D:\home\site\wwwroot\server.js
require() of ES modules is not supported.
require() of D:\home\site\wwwroot\server.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 "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename server.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from D:\home\site\wwwroot\package.json.

Checked the azure app service, WEBSITE_NODE_DEFAULT_VERSION is set to ~14, and the installed nodejs version is v14.15.0 in the web app. Don't think this version of node has problem with import export anymore.

The code runs perfectly fine locally with either

node server.js 
# or
node --experimental-modules server

Not sure why it is failing in azure

My code is below:

server.js

import express from 'express';
import bodyParser from 'body-parser';

let app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

let port = process.env.PORT || 8080;
let router = express.Router(); 

// Middleware to use for all requests
router.use(function(req, res, next) {
    next();
});

router.get('/', function(req, res) {
    res.json({ message: 'Default home page for the api!' });
});

app.use('/api', router);

// START THE SERVER
// =============================================================================
app.listen(port);

console.log(`Server up and running on port ${port}`);

package.json

{
    "name": "my_package",
    "version": "1.0.0",
    "type": "module",
    "scripts": {
      "start": "node --experimental-modules server",
      "test": "mocha"
    },
    "dependencies": {
      "body-parser": "^1.19.0",
      "express": "^4.17.1"
    }
  }
  

However, if I change the import & export to require, then it will run in azure web app, it seems like perhaps azure iisnode is not patible with emac6 yet? Anyone knows?

Anyone has any work around of this beside using babel to transpile emac6 down to emac5? As I have having some problem with executing and running the transpiled emac5 code.

Thankz

Share edited Jan 14, 2021 at 4:10 Ohhh asked Jan 13, 2021 at 13:58 OhhhOhhh 4352 gold badges9 silver badges26 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 7

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.

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"
}

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744809195a268181.html

最新回复(0)