Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

817
Visualizações
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 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 compatible 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

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda