[SOLVED] I have an error message when launching an express app


I have this code, that should normally work :

import express from "express";
import nunjucks from "nunjucks";
import fetch from "node-fetch";

const app = express();


nunjucks.configure("views", {
  autoescape: true,
  express: app,
app.set("view engine", "njk");

app.get("/", (req, response) => {
    .then((response) => response.json())
    .then((data) =>
      // response.render("home", { categories: data }

app.listen(3000, () => {
  console.log("Server started on http://localhost:3000");

but when I try to run it with ts-node src/index.ts, i have this error :

Error [ERR_REQUIRE_ESM]: require() of ES Module /mnt/c/Users/me/Desktop/CODE/sw-api/node_modules/node-fetch/src/index.js from /mnt/c/Users/me/Desktop/CODE/sw-api/src/index.ts not supported.
Instead change the require of index.js in /mnt/c/Users/me/Desktop/CODE/sw-api/src/index.ts to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/mnt/c/Users/me/Desktop/CODE/sw-api/src/index.ts:9:36)
    at Module.m._compile (/mnt/c/Users/me/Desktop/CODE/sw-api/node_modules/ts-node/dist/index.js:735:29)
    at Object.require.extensions.<computed> [as .ts] (/mnt/c/Users/me/Desktop/CODE/sw-api/node_modules/ts-node/dist/index.js:737:16)
    at main (/mnt/c/Users/me/Desktop/CODE/sw-api/node_modules/ts-node/dist/bin.js:238:16)
    at Object.<anonymous> (/mnt/c/Users/me/Desktop/CODE/sw-api/node_modules/ts-node/dist/bin.js:351:5) {

I don’t quite understand, cause i don’t use require() to import my modules

Could anyone help me ? Thank you


If you haven’t, try updating package.json with "type": "module". Also take a look at the following: Error: require() of ES modules is not supported when importing node-fetch and Error [ERR_REQUIRE_ESM]: require() of ES Module not supported.

Answered By – perthos

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *