__awaiter is not defined when using async/await in Typescript
When you use some functionalities from future version of JavaScript (ES6 and beyond) like in your case async/await
, TypeScript
generates helper functions. These helper functions are used to provide the new functionalities as ES5 code, thus it can be run in a web browser.
Your problem:
In your tsconfig.json
you set the noEmitHelpers
value to true
. By doing that you tell the TypeScript
compiler that you will provide these helper functions yourself.
How to fix it:
- You can set the
noEmitHelpers
value tofalse
in yourtsconfig.json
, thus theTypeScript
compiler will emit the helper functions when needed. One drawback of this method is that if you use for exampleasync/await
in 2 different files, the helper functions will be emitted 2 times (one per file). - The other solution is to set the
--importHelpers
flag when you usetsc
. It will tell theTypeScript
compiler to include the helper functions only once. Please note that if you use this solution you have to install thetslib
package.
In your case: tsc --importHelpers -w
The accepted answer didn't work in my case, but I found that my tsconfig.json
was targeting es6
("target":"es6"
).
What this means is that TypeScript transpiles to code that uses the __awaiter
util because async
await
was not included in the spec until ES2017.
I fixed this by changing my target
to ESNext
(or anything ES2017
and above)