javascript - ReferenceError: exports is not defined in ES module scope - Stack Overflow

admin2025-04-19  0

I am new in typescript, when I piled code it piled properly but when I run program using node I got this error ReferenceError: exports is not defined in ES module scope


ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and 'D:\projects\pro8\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///D:/projects/pro8/await.js:40:1
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

typescript code

import fetch from "node-fetch";

const baseApi = ";;
const userApi = ";;
interface Employee {
    id: number
    employee_name: string
    employee_salary: number
    employee_age: number
    profile_image: string
}
const fetchAllEmployees = async (url: string): Promise<Employee[]> => {
  const response = await fetch(url);
  const { data }:any = await response.json();
  return data;
};

const fetchEmployee = async (url: string, id: number): Promise<Record<string, string>> => {
  const response = await fetch(`${url}/${id}`);
  const { data }:any = await response.json();
  return data;
};
const generateEmail = (name: string): string => {
  return `${name.split(" ").join(".")}@pany`;
};

const runAsyncFunctions = async () => {
  try {
    const employees = await fetchAllEmployees(baseApi);
    Promise.all(
      employees.map(async user => {
        const userName = await fetchEmployee(userApi, user.id);
        const emails = generateEmail(userName.name);
        return emails;
      })
    );
  } catch (error) {
    console.log(error);
  }
};
runAsyncFunctions();

package.json

{
  "name": "pro8",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.21.0",
    "@typescript-eslint/parser": "^5.21.0",
    "eslint": "^8.14.0",
    "eslint-config-standard": "^17.0.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-n": "^15.2.0",
    "eslint-plugin-promise": "^6.0.0"
  },
  "dependencies": {
    "@types/node-fetch": "^2.6.1",
    "node-fetch": "^3.2.4"
  }
}

I tried to solve this error ts An async function or method in ES5/ES3 requires the 'Promise' constructor

any answer not working in my case

so please give me solution if any

I am new in typescript, when I piled code it piled properly but when I run program using node I got this error ReferenceError: exports is not defined in ES module scope


ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and 'D:\projects\pro8\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///D:/projects/pro8/await.js:40:1
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

typescript code

import fetch from "node-fetch";

const baseApi = "https://reqres.in/api/users?page=1";
const userApi = "https://reqres.in/api/user";
interface Employee {
    id: number
    employee_name: string
    employee_salary: number
    employee_age: number
    profile_image: string
}
const fetchAllEmployees = async (url: string): Promise<Employee[]> => {
  const response = await fetch(url);
  const { data }:any = await response.json();
  return data;
};

const fetchEmployee = async (url: string, id: number): Promise<Record<string, string>> => {
  const response = await fetch(`${url}/${id}`);
  const { data }:any = await response.json();
  return data;
};
const generateEmail = (name: string): string => {
  return `${name.split(" ").join(".")}@pany.`;
};

const runAsyncFunctions = async () => {
  try {
    const employees = await fetchAllEmployees(baseApi);
    Promise.all(
      employees.map(async user => {
        const userName = await fetchEmployee(userApi, user.id);
        const emails = generateEmail(userName.name);
        return emails;
      })
    );
  } catch (error) {
    console.log(error);
  }
};
runAsyncFunctions();

package.json

{
  "name": "pro8",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.21.0",
    "@typescript-eslint/parser": "^5.21.0",
    "eslint": "^8.14.0",
    "eslint-config-standard": "^17.0.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-n": "^15.2.0",
    "eslint-plugin-promise": "^6.0.0"
  },
  "dependencies": {
    "@types/node-fetch": "^2.6.1",
    "node-fetch": "^3.2.4"
  }
}

I tried to solve this error ts An async function or method in ES5/ES3 requires the 'Promise' constructor

any answer not working in my case

so please give me solution if any

Share asked May 2, 2022 at 12:09 shivam20 Yadavshivam20 Yadav 831 gold badge2 silver badges6 bronze badges 4
  • at file:///D:/projects/pro8/await.js:40:1 – hoangdv Commented May 2, 2022 at 12:53
  • @hoangdv don't know what you talking about can you please elaborate on it – shivam20 Yadav Commented May 2, 2022 at 14:06
  • This is the main information about your error. In other words, the error may bee from this file, so let's check it. Something went wrong, you have a js file in a typescript project. – hoangdv Commented May 2, 2022 at 14:10
  • What is the contents of index.js? What is the node mand you are trying to execute? Since it is a typescript file, you need to check what is generated source code, try to paste generated source code of the main typescript file as well. – Akash Kava Commented Nov 5, 2023 at 8:30
Add a ment  | 

1 Answer 1

Reset to default 1

Try and remove "type": "module" from package.json. I had this issue before and it seemed to do the trick.

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

最新回复(0)