7

I am new in typescript, when I compiled code it compiled 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(".")}@company.com`;
};

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

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 Commented May 2, 2022 at 14:06
  • This is the main information about your error. In other words, the error may become 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 command 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

1 Answer 1

1

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

5
  • 1
    I tried this not working getting other error " ts An async function or method in ES5/ES3 requires the 'Promise' constructor" but it works without promises and changing in tsconfig file "target": "es2016" Commented May 3, 2022 at 5:22
  • This seemed to work for me!
    – MsNichols
    Commented Aug 10, 2023 at 3:30
  • 2
    can you explain why should we do like this?
    – Dolphin
    Commented Oct 21, 2023 at 1:04
  • 2
    This is not a sufficient answer, please explain more.
    – JamesD
    Commented Oct 23, 2023 at 2:42
  • Explain please OP
    – Denny
    Commented Mar 3 at 0:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.