ts An async function or method in ES5/ES3 requires the 'Promise' constructor

TypescriptAsync Await

Typescript Problem Overview


Hello I'm Using async/await in my TypeScript Project, But I Get this log:

[ts] An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your --lib option.

How Can I Solve That?

Typescript Solutions


Solution 1 - Typescript

As the error message says, add lib: es2015 to your tsconfig.json

// tsconfig.json
{
  "compilerOptions": {
    "lib": [ "es2015" ]
  }
}

UPDATE: if this doesn't work for you, try this:

JetBrains IDE such as WebStorm, use its own implementation by default. Make sure you configure it to use TypeScript language service instead.

For Visual Studio, the project files and tsconfig.json are mutually exclusive. You will need to configure your project directly.

<https://github.com/Microsoft/TypeScript/issues/3983#issuecomment-123861491>

Solution 2 - Typescript

Try this package which contains type definitions for es6-promise

npm install --save @types/es6-promise

Solution 3 - Typescript

If you are on VS, delete the tsconfig.json and right click the project in Solution Explorer, then click on Properties->TypeScript Build in General change the followings

  • ECMAScript version: ECMAScript 6

  • Module System: ES2015

Solution 4 - Typescript

For me the error occurred in my testing files inside src/tests folder. Since I use ts-node for testing the .ts files directly, I excluded src/tests/* in my tsconfig.json. As soon as I deleted the line the error was gone (which makes sense in the end).

Just in case someone else is struggling with this in his testing files.

EDIT: Of course you need to configure your --lib option correctly as outlined in the accepted answer. My tsconfig.json --lib option works as following:

"lib": [
    "es2018",
    "dom"
]

Solution 5 - Typescript

Add "es2015.promise" in tsconfig.json - in "lib" = like this :

{
    "compilerOptions": {
        "target": "es5",
        "lib": [
            "es5",
            "dom",
            "es2015.promise"
        ],
        "types": [
            "mocha"
        ],
        "module": "commonjs",
        "outDir": "../../../out",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "sourceMap": true,
        "declaration": true
    },
    "exclude": [
        "node_modules",
        "dist",
        "tests"
    ]
}

Solution 6 - Typescript

You could also use the "lib": "es2015.promise" for that specific error

Solution 7 - Typescript

VS2019 does not seem to recognize the tsconfig.json file, so LIB options will not change the application. This is a way to add the PROMISE for typescript to accept ASYNC AWAIT.

export function AD(first: any, second: any, callBack: any)
{
    const rtn = async (dispatch: any): Promise<void> =>
    {
        await axios.post(TYPE.URI, { // provide a string of a URI to post into
            parm1: first,
            parm2: second
        })
            .then(data => // or you can use response instead of data as a name
            {
                console.log("data from call next");
                console.log(data);
                dispatch({ type: TYPES.AD, payload: data.data });
                if (callBack)
                {
                    callBack(data);
                }
            })
    }
    return rtn;

}

Solution 8 - Typescript

In my case, I had simply accidentally deleted my "return" statement in the function, and this error was showing until I put it back.

Solution 9 - Typescript

When you compile or watch your typescript file via tsc index.ts --watch add --lib flag to it with its value (in our case es2015), it should look like this
tsc index.ts --watch --lib es2015
for better work tsc index.ts --watch --lib "es2015, es2016, dom" - when it can't read dom from tsconfig.json

Solution 10 - Typescript

I've finally managed to solve it!
My command on the terminal: yarn tsc main.ts && nodejs main.js My error message:

main.ts:1:16 - error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor.  Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.

1 async function main() {
                 ~~~~


Found 2 errors.

error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

What I did to solve it, was referencing the tsconfig.json file.
My tsconfig.json file was like this:

{
    "compilerOptions": {
        "target": "ESNext",
        "lib": [
            "ES2015",
            "DOM"
        ]
    }
}

And my terminal command is like this: yarn tsc -p ./tsconfig.json && nodejs main.js If I want to run other .ts file I simply do: yarn tsc -p ./tsconfig.json && nodejs file_name.js

Solution 11 - Typescript

tsc --target ES6

This was the solution for me !

Solution 12 - Typescript

I am using VS2017 v15.8.2 and Typescript 2.4.2 in an Angular 4 project (under a class library project in my solution, not a typescript project). I was able to remove the error/warning in VS by disabling the JavaScript language service:

Options => Text Editor => JavaScript/TypeScript => Language Service

Restart VS.

Hope this helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaeed HeidarizareiView Question on Stackoverflow
Solution 1 - TypescriptunionalView Answer on Stackoverflow
Solution 2 - TypescriptJeff HernandezView Answer on Stackoverflow
Solution 3 - TypescriptIvandro JaoView Answer on Stackoverflow
Solution 4 - Typescriptbene-weView Answer on Stackoverflow
Solution 5 - TypescriptGuruprasad GVView Answer on Stackoverflow
Solution 6 - TypescriptRannie Aguilar PeraltaView Answer on Stackoverflow
Solution 7 - TypescriptsefView Answer on Stackoverflow
Solution 8 - TypescriptraterusView Answer on Stackoverflow
Solution 9 - TypescriptALbert2504View Answer on Stackoverflow
Solution 10 - TypescriptDr4kk0nnysView Answer on Stackoverflow
Solution 11 - TypescriptrambiView Answer on Stackoverflow
Solution 12 - TypescriptValoneView Answer on Stackoverflow